Yes, the ternary operator confuses a lot of people who are new to coding. It's like "Where the heck did that come from?"
Basically the '?' is the first half of the ternary operator and the ':' is the second half. It works something like this:
boolean ? expression_1 : expression_2
Which evaluates to expression_1 if boolean is true, or expression_2 if boolean is false. It's really just shorthand in most cases. For example,
return (max >= height / 2 ? (short)(width-1) : short.MinValue);
either returns (short)(width-1) if max is greater than or equal to half of height, or returns short.MinValue otherwise. It could be written as
if (max >= height / 2)
return (short)(width-1);
else
return short.MinValue;
but it's sometimes simpler to write things like this on one line without needing to write unnecessary code.
Here's a sample for TopHalf (not tested).
public class TopHalf : TileShape
{
private static TopHalf m_Value = new TopHalf();
public static TopHalf Value
{
get
{
return m_Value;
}
}
public TopHalf()
{
}
public override short GetTopSolidPixel(short width, short height, short min, short max)
{
return (short)0;
}
public override short GetBottomSolidPixel(short width, short height, short min, short max)
{
return (short)(height/2);
}
public override short GetLeftSolidPixel(short width, short height, short min, short max)
{
return (max < height / 2 ? (short)0 : short.MaxValue);
}
public override short GetRightSolidPixel(short width, short height, short min, short max)
{
return (max < height / 2 ? (short)(width-1) : short.MaxValue);
}
}