Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.Drawing;
- namespace ui_elements {
- class DropDown {
- List<string> items;
- Point startPosition;
- Size size;
- int itemHeight;
- int selectedId;
- bool isDropDownVisible;
- string defaultText;
- internal DropDown(Point startPosition, Size size, int itemHeight,
- string defaultText) {
- this.startPosition = startPosition;
- this.size = size;
- this.itemHeight = itemHeight;
- items = new List<string>();
- this.defaultText = defaultText;
- }
- internal List<string> Items {
- get { return items; }
- set { items = value; }
- }
- internal Point StartPosition {
- get { return startPosition; }
- set { startPosition = value; }
- }
- internal Size Size {
- get { return size; }
- set { size = value; }
- }
- internal int ItemHeight {
- get { return itemHeight; }
- set { itemHeight = value; }
- }
- internal int SelectedId {
- get { return selectedId; }
- set {
- if ( value >= 0 && value < items.Count ) {
- selectedId = value;
- }
- }
- }
- internal bool IsDropDownVisible {
- get { return isDropDownVisible; }
- set { isDropDownVisible = value; }
- }
- internal void ToogleDropDownVisible() {
- isDropDownVisible = !isDropDownVisible;
- }
- internal string DefaultText {
- get { return defaultText; }
- set { defaultText = value; }
- }
- internal void Draw(Graphics g) {
- Rectangle dropDownWrap = new Rectangle(startPosition, size);
- Pen wrapPen = new Pen(new SolidBrush(Color.LightGray));
- g.DrawRectangle(wrapPen, dropDownWrap);
- int trWidth = size.Height / 2;
- int teStartX = startPosition.X - size.Height / 4 + size.Width;
- int teStartY = startPosition.Y + (int) ( size.Height / 2.5F );
- g.FillPolygon(new SolidBrush(Color.LightGray),
- new Point[] {
- new Point(teStartX, teStartY),
- new Point(teStartX - trWidth,teStartY),
- new Point(teStartX - trWidth/2, teStartY + (int)(trWidth * 0.7F)),
- new Point(teStartX, teStartY)
- });
- int fontSizeInPx = (int) ( g.DpiX / 72.0F * Graphic.MaintFont.SizeInPoints );
- string selectedItemText = defaultText;
- if ( items != null && items.Count > 0 && !isDropDownVisible ) {
- selectedItemText = items[selectedId];
- }
- int textStartPointX = startPosition.X + size.Height / 4;
- int textStartPointY = startPosition.Y + ( size.Height - fontSizeInPx ) / 2;
- Point textStartPoint = new Point(textStartPointX, textStartPointY);
- Brush textBrush = new SolidBrush(Color.DimGray);
- g.DrawString(selectedItemText, Graphic.MaintFont, textBrush, textStartPoint);
- if ( IsDropDownVisible ) {
- drawDropDownOptions(g, fontSizeInPx);
- }
- }
- private void drawDropDownOptions(Graphics g, int fontSizeInPx) {
- Rectangle dropDown = new Rectangle(
- startPosition.X, startPosition.Y + size.Height,
- size.Width, itemHeight * items.Count);
- Pen pen = new Pen(new SolidBrush(Color.LightGray));
- Brush brush = new SolidBrush(Color.White);
- g.FillRectangle(brush, dropDown);
- int x = startPosition.X + itemHeight / 4;
- int y = startPosition.Y + size.Height +
- ( itemHeight - fontSizeInPx ) / 2 - itemHeight;
- int lineX = startPosition.X;
- int lineY = startPosition.Y + size.Height;
- for ( int i = 0; i < items.Count; i++ ) {
- Point itemPt1 = new Point(lineX, lineY += itemHeight);
- Point itemPt2 = new Point(lineX + size.Width, lineY);
- Point textPoint = new Point(x, y += itemHeight);
- Brush textBrush = new SolidBrush(Color.Black);
- g.DrawLine(pen, itemPt1, itemPt2);
- g.DrawString(items[i], Graphic.MaintFont, textBrush, textPoint);
- }
- g.DrawRectangle(pen, dropDown);
- }
- internal bool SelectItem(Point point) {
- if ( isDropDownVisible && point.X > startPosition.X &&
- point.X < startPosition.X + size.Width ) {
- SelectedId = ( point.Y - startPosition.Y - Size.Height ) / itemHeight;
- return true;
- }
- return false;
- }
- internal bool IsHitPoint(Point point) {
- return point.X > startPosition.X &&
- point.X < startPosition.X + size.Width &&
- point.Y > startPosition.Y &&
- point.Y < startPosition.Y + size.Height;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement