/* AddIcon, makes an icon to represent a given modelTexture and adds it * to the grid of icons. * * @param modelTexture * @param selected, whether the icon should be selected or not */ public void AddIcon(ModelTexture modelTexture, bool selected) { //Build Icon Prefab GameObject IconGO = Instantiate(ColorIconPrefab); AddElement(IconGO); //Setup Icon ColorIcon icon = IconGO.GetComponent <ColorIcon>(); icon.Selected = false; icon.ModelTexture = modelTexture; if (icon.ClickBox != null) { icon.ClickBox.AddEventListener("onclick", () => { Selected = icon; RunEvent("onselect"); }); } }
public static ColorIcon[] GetColorIcons(Stream resourceStream, int colorIc, bool bigEndian, Encoding encoding) { byte[] buffer; if (colorIc == -1 || colorIc >= resourceStream.Length) { return(null); } resourceStream.Position = colorIc; int cicons = 0; while (true) { buffer = new byte[4]; resourceStream.Read(buffer, 0, buffer.Length); if (BitConverter.ToInt32(buffer, 0) == -1) { break; } cicons++; } ColorIcon[] colorIcons = new ColorIcon[cicons]; for (int i = 0; i < cicons; i++) { buffer = new byte[Marshal.SizeOf(typeof(IconBlock))]; resourceStream.Read(buffer, 0, buffer.Length); IconBlock iconBlock = BigEndianMarshal.ByteArrayToStructureBigEndian <IconBlock>(buffer); int isize = iconBlock.ib_wicon * iconBlock.ib_hicon / 8; buffer = new byte[4]; resourceStream.Position -= 2; resourceStream.Read(buffer, 0, buffer.Length); int numRez = BitConverter.ToInt32(buffer.Reverse().ToArray(), 0); colorIcons[i] = new ColorIcon { Color = new ColorIconPlane[numRez], Monochrome = new Icon { Width = iconBlock.ib_wicon, Height = iconBlock.ib_hicon, X = iconBlock.ib_xicon, Y = iconBlock.ib_yicon, ForegroundColor = (ObjectColors)((iconBlock.ib_char >> 12) & 0x000F), BackgroundColor = (ObjectColors)((iconBlock.ib_char >> 8) & 0x000F), Character = encoding.GetString(new[] { (byte)(iconBlock.ib_char & 0xFF) })[0], CharX = iconBlock.ib_xchar, CharY = iconBlock.ib_ychar, TextX = iconBlock.ib_xtext, TextY = iconBlock.ib_ytext, TextWidth = iconBlock.ib_wtext, TextHeight = iconBlock.ib_htext, Data = new byte[isize], Mask = new byte[isize] } }; resourceStream.Read(colorIcons[i].Monochrome.Data, 0, isize); // Because the image is stored as words, they get reversed on PC GEM (Little-endian) if (!bigEndian) { byte[] data = new byte[colorIcons[i].Monochrome.Data.Length]; for (int d = 0; d < data.Length; d += 2) { data[d] = colorIcons[d].Monochrome.Data[d + 1]; data[d + 1] = colorIcons[d].Monochrome.Data[d]; } colorIcons[i].Monochrome.Data = data; } resourceStream.Read(colorIcons[i].Monochrome.Mask, 0, isize); // Because the mask is stored as words, they get reversed on PC GEM (Little-endian) if (!bigEndian) { byte[] mask = new byte[colorIcons[i].Monochrome.Mask.Length]; for (int m = 0; m < mask.Length; m += 2) { mask[m] = colorIcons[m].Monochrome.Mask[m + 1]; mask[m + 1] = colorIcons[m].Monochrome.Mask[m]; } colorIcons[i].Monochrome.Mask = mask; } if (iconBlock.ib_ptext > 0 && iconBlock.ib_ptext < resourceStream.Length) { long oldPosition = resourceStream.Position; resourceStream.Position = iconBlock.ib_ptext; List <byte> chars = new List <byte>(); while (true) { int character = resourceStream.ReadByte(); if (character <= 0) { break; } chars.Add((byte)character); } colorIcons[i].Monochrome.Text = StringHandlers.CToString(chars.ToArray(), encoding); resourceStream.Position = oldPosition + 12; } else { byte[] ptext = new byte[12]; resourceStream.Read(ptext, 0, 12); colorIcons[i].Monochrome.Text = StringHandlers.CToString(ptext, encoding); } colorIcons[i].Color = new ColorIconPlane[numRez]; for (int r = 0; r < numRez; r++) { byte[] data; byte[] mask; buffer = new byte[Marshal.SizeOf(typeof(ColorIconBlock))]; resourceStream.Read(buffer, 0, buffer.Length); ColorIconBlock cib = BigEndianMarshal.ByteArrayToStructureBigEndian <ColorIconBlock>(buffer); colorIcons[i].Color[r] = new ColorIconPlane { Planes = cib.num_planes, Data = new byte[isize * cib.num_planes], Mask = new byte[isize] }; resourceStream.Read(colorIcons[i].Color[r].Data, 0, isize * cib.num_planes); // Because the image is stored as words, they get reversed on PC GEM (Little-endian) if (!bigEndian) { data = new byte[colorIcons[i].Color[r].Data.Length]; for (int d = 0; d < data.Length; d += 2) { data[d] = colorIcons[d].Color[r].Data[d + 1]; data[d + 1] = colorIcons[d].Color[r].Data[d]; } colorIcons[i].Color[r].Data = data; } resourceStream.Read(colorIcons[i].Color[r].Mask, 0, isize); // Because the mask is stored as words, they get reversed on PC GEM (Little-endian) if (!bigEndian) { mask = new byte[colorIcons[i].Color[r].Mask.Length]; for (int m = 0; m < mask.Length; m += 2) { mask[m] = colorIcons[m].Color[r].Mask[m + 1]; mask[m + 1] = colorIcons[m].Color[r].Mask[m]; } colorIcons[i].Color[r].Mask = mask; } if (cib.sel_data == 0) { continue; } colorIcons[i].Color[r].SelectedData = new byte[isize * cib.num_planes]; colorIcons[i].Color[r].SelectedMask = new byte[isize]; resourceStream.Read(colorIcons[i].Color[r].SelectedData, 0, isize * cib.num_planes); // Because the image is stored as words, they get reversed on PC GEM (Little-endian) if (!bigEndian) { data = new byte[colorIcons[i].Color[r].SelectedData.Length]; for (int d = 0; d < data.Length; d += 2) { data[d] = colorIcons[d].Color[r].SelectedData[d + 1]; data[d + 1] = colorIcons[d].Color[r].SelectedData[d]; } colorIcons[i].Color[r].SelectedData = data; } resourceStream.Read(colorIcons[i].Color[r].SelectedMask, 0, isize); // Because the mask is stored as words, they get reversed on PC GEM (Little-endian) if (bigEndian) { continue; } mask = new byte[colorIcons[i].Color[r].SelectedMask.Length]; for (int m = 0; m < mask.Length; m += 2) { mask[m] = colorIcons[m].Color[r].SelectedMask[m + 1]; mask[m + 1] = colorIcons[m].Color[r].SelectedMask[m]; } colorIcons[i].Color[r].SelectedMask = mask; } } return(colorIcons); }