Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Drawing;
  3.  
  4. namespace ui_elements {
  5. class DropDown {
  6. List<string> items;
  7. Point startPosition;
  8. Size size;
  9. int itemHeight;
  10. int selectedId;
  11. bool isDropDownVisible;
  12. string defaultText;
  13.  
  14. internal DropDown(Point startPosition, Size size, int itemHeight,
  15. string defaultText) {
  16. this.startPosition = startPosition;
  17. this.size = size;
  18. this.itemHeight = itemHeight;
  19. items = new List<string>();
  20. this.defaultText = defaultText;
  21. }
  22.  
  23. internal List<string> Items {
  24. get { return items; }
  25. set { items = value; }
  26. }
  27.  
  28. internal Point StartPosition {
  29. get { return startPosition; }
  30. set { startPosition = value; }
  31. }
  32.  
  33. internal Size Size {
  34. get { return size; }
  35. set { size = value; }
  36. }
  37.  
  38. internal int ItemHeight {
  39. get { return itemHeight; }
  40. set { itemHeight = value; }
  41. }
  42.  
  43. internal int SelectedId {
  44. get { return selectedId; }
  45. set {
  46. if ( value >= 0 && value < items.Count ) {
  47. selectedId = value;
  48. }
  49. }
  50. }
  51.  
  52. internal bool IsDropDownVisible {
  53. get { return isDropDownVisible; }
  54. set { isDropDownVisible = value; }
  55. }
  56.  
  57. internal void ToogleDropDownVisible() {
  58. isDropDownVisible = !isDropDownVisible;
  59. }
  60.  
  61. internal string DefaultText {
  62. get { return defaultText; }
  63. set { defaultText = value; }
  64. }
  65.  
  66. internal void Draw(Graphics g) {
  67. Rectangle dropDownWrap = new Rectangle(startPosition, size);
  68. Pen wrapPen = new Pen(new SolidBrush(Color.LightGray));
  69. g.DrawRectangle(wrapPen, dropDownWrap);
  70.  
  71. int trWidth = size.Height / 2;
  72. int teStartX = startPosition.X - size.Height / 4 + size.Width;
  73. int teStartY = startPosition.Y + (int) ( size.Height / 2.5F );
  74.  
  75. g.FillPolygon(new SolidBrush(Color.LightGray),
  76. new Point[] {
  77. new Point(teStartX, teStartY),
  78. new Point(teStartX - trWidth,teStartY),
  79. new Point(teStartX - trWidth/2, teStartY + (int)(trWidth * 0.7F)),
  80. new Point(teStartX, teStartY)
  81. });
  82.  
  83. int fontSizeInPx = (int) ( g.DpiX / 72.0F * Graphic.MaintFont.SizeInPoints );
  84.  
  85. string selectedItemText = defaultText;
  86. if ( items != null && items.Count > 0 && !isDropDownVisible ) {
  87. selectedItemText = items[selectedId];
  88. }
  89.  
  90. int textStartPointX = startPosition.X + size.Height / 4;
  91. int textStartPointY = startPosition.Y + ( size.Height - fontSizeInPx ) / 2;
  92. Point textStartPoint = new Point(textStartPointX, textStartPointY);
  93. Brush textBrush = new SolidBrush(Color.DimGray);
  94.  
  95. g.DrawString(selectedItemText, Graphic.MaintFont, textBrush, textStartPoint);
  96.  
  97. if ( IsDropDownVisible ) {
  98. drawDropDownOptions(g, fontSizeInPx);
  99. }
  100. }
  101.  
  102. private void drawDropDownOptions(Graphics g, int fontSizeInPx) {
  103. Rectangle dropDown = new Rectangle(
  104. startPosition.X, startPosition.Y + size.Height,
  105. size.Width, itemHeight * items.Count);
  106. Pen pen = new Pen(new SolidBrush(Color.LightGray));
  107. Brush brush = new SolidBrush(Color.White);
  108.  
  109. g.FillRectangle(brush, dropDown);
  110.  
  111. int x = startPosition.X + itemHeight / 4;
  112. int y = startPosition.Y + size.Height +
  113. ( itemHeight - fontSizeInPx ) / 2 - itemHeight;
  114. int lineX = startPosition.X;
  115. int lineY = startPosition.Y + size.Height;
  116.  
  117. for ( int i = 0; i < items.Count; i++ ) {
  118. Point itemPt1 = new Point(lineX, lineY += itemHeight);
  119. Point itemPt2 = new Point(lineX + size.Width, lineY);
  120. Point textPoint = new Point(x, y += itemHeight);
  121. Brush textBrush = new SolidBrush(Color.Black);
  122.  
  123. g.DrawLine(pen, itemPt1, itemPt2);
  124. g.DrawString(items[i], Graphic.MaintFont, textBrush, textPoint);
  125. }
  126.  
  127. g.DrawRectangle(pen, dropDown);
  128. }
  129.  
  130. internal bool SelectItem(Point point) {
  131. if ( isDropDownVisible && point.X > startPosition.X &&
  132. point.X < startPosition.X + size.Width ) {
  133. SelectedId = ( point.Y - startPosition.Y - Size.Height ) / itemHeight;
  134.  
  135. return true;
  136. }
  137.  
  138. return false;
  139. }
  140.  
  141. internal bool IsHitPoint(Point point) {
  142. return point.X > startPosition.X &&
  143. point.X < startPosition.X + size.Width &&
  144. point.Y > startPosition.Y &&
  145. point.Y < startPosition.Y + size.Height;
  146. }
  147.  
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement