Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace Fogger2
- {
- public partial class fogFunction : UserControl
- {
- private string rawString;
- public fogFunction(string value, FunctionType type1)
- {
- InitializeComponent();
- rawString = value;
- func = FunctionNode.fromString(value);
- ftype = type1;
- FunctionNode fn = func;
- while (null != fn)
- {
- if (fn is SlotNode)
- {
- SlotNode sn = fn as SlotNode;
- Controls.Add(sn.getCardValue());
- }
- fn = fn.getNext();
- }
- slotIndex = 0;
- DrawMathString(CreateGraphics(), value, false, 16.0f, 32.0f);
- Paint += new PaintEventHandler(fogFunction_Paint);
- }
- void fogFunction_Paint(object sender, PaintEventArgs e)
- {
- slotIndex = 0;
- DrawMathString(e.Graphics, rawString, true, 16.0f, 32.0f);
- }
- enum TokenType
- {
- TT_SQRT,
- TT_POW,
- TT_SLOT,
- };
- RectangleF merge(RectangleF l, RectangleF r)
- {
- float[] points =
- { Math.Min( l.Left, r.Left )
- , Math.Min( l.Top, r.Top )
- , Math.Max( l.Right, r.Right )
- , Math.Max( l.Bottom, r.Bottom )
- };
- return new RectangleF(points[0], points[1], points[2] - points[0], points[3] - points[1]);
- }
- int slotIndex = 0;
- RectangleF DrawMathString(Graphics g, string str, bool draw = true, float offset_x = 0.0f, float offset_y = 0.0f, float scale = 1.0f)
- {
- RectangleF coordsOut = new RectangleF(offset_x, offset_y, 0, 0);
- Font f = new Font(Font.FontFamily, Font.Size * scale);
- float cursor_x = offset_x;
- float cursor_y = offset_y;
- int slotHeight = 50;
- int slotWidth = 100;
- int slotOffset = (slotHeight - Font.Height) / 2;
- for (int i = 0; i < str.Length; ++i)
- {
- TokenType tt = TokenType.TT_POW;
- string this_draw = "";
- char c = str[i];
- switch (c)
- {
- case 's':
- if (str.Length - i < 4
- || str[i + 1] != 'q'
- || str[i + 2] != 'r'
- || str[i + 3] != 't')
- {
- this_draw += c;
- break;
- }
- else
- {
- i += 3;
- tt = TokenType.TT_SQRT;
- }
- goto case '^';
- case '^':
- i += 1;
- if (str[i] != '(')
- {
- MessageBox.Show("Error: ^ or sqrt must be proceeded by (");
- }
- else
- {
- int open_brackets = 1;
- int j = i + 1;
- for (j = i + 1; j < str.Length; ++j)
- {
- if (str[j] == '(')
- ++open_brackets;
- else if (str[j] == ')')
- {
- if (0 == --open_brackets)
- break;
- }
- }
- if (0 != open_brackets)
- {
- MessageBox.Show("Error: bracket was not correctly closed");
- }
- string subString = str.Substring(i + 1, j - i - 1);
- if (tt == TokenType.TT_POW)
- {
- float offset = Font.Height * 0.4f;
- RectangleF bounds = DrawMathString(g, subString, draw, cursor_x, cursor_y - offset, scale * 0.8f);
- coordsOut = merge(coordsOut, bounds);
- cursor_x += bounds.Width;
- }
- else
- {
- float sqrt_length = 14;
- RectangleF bounds = DrawMathString(g, subString, draw, cursor_x + sqrt_length, cursor_y, scale);
- float top = bounds.Top - 4;
- float bottom = bounds.Bottom + 4;
- float flick_height = bottom - 8;
- float first_x = cursor_x + 3;
- float second_x = cursor_x + 6;
- float third_x = bounds.Right + 2;
- if (draw)
- {
- g.DrawLine(SystemPens.ControlText, cursor_x, flick_height, first_x, bottom);
- g.DrawLine(SystemPens.ControlText, first_x, bottom, second_x, top);
- g.DrawLine(SystemPens.ControlText, second_x, top, third_x, top);
- }
- bounds.X = cursor_x;
- bounds.Width = third_x - cursor_x + 8;
- bounds.Y = bottom;
- bounds.Height += top - bottom;
- cursor_x += bounds.Width;
- coordsOut = merge(coordsOut, bounds);
- }
- i = j;
- }
- break;
- case '@':
- RectangleF rect = new RectangleF(cursor_x, cursor_y - slotOffset, slotWidth, slotHeight);
- coordsOut = merge(coordsOut, rect);
- if (!draw)
- {
- Controls[slotIndex].SetBounds((int)cursor_x, (int)cursor_y - slotOffset, (int)slotWidth, (int)slotHeight);
- }
- ++slotIndex;
- cursor_x += slotWidth;
- break;
- default:
- this_draw += c;
- break;
- }
- if (this_draw.Length > 0)
- {
- if (draw)
- {
- g.DrawString(this_draw, f, SystemBrushes.ControlText, cursor_x, cursor_y);
- }
- SizeF sz = g.MeasureString(this_draw, f);
- RectangleF rect = new RectangleF(cursor_x, cursor_y, sz.Width, sz.Height);
- coordsOut = merge(coordsOut, rect);
- cursor_x += sz.Width;
- }
- }
- return coordsOut;
- }
- public void setCorrectCard(int slot, Card card)
- {
- FunctionNode currentNode = func;
- int count = 0;
- while (currentNode != null)
- {
- if (currentNode is SlotNode)
- {
- count += 1;
- if (count == slot)
- {
- (currentNode as SlotNode).setCorrectCard(card);
- }
- }
- currentNode = currentNode.getNext();
- }
- }
- public void setCard(int slot, Card card)
- {
- FunctionNode currentNode = func;
- int count = 0;
- while (currentNode != null)
- {
- if (currentNode is SlotNode)
- {
- count += 1;
- if (count == slot)
- {
- (currentNode as SlotNode).setCard(card);
- }
- }
- currentNode = currentNode.getNext();
- }
- }
- public bool validateSolution()
- {
- FunctionNode fn = func;
- while (null != fn)
- {
- if (fn is SlotNode)
- {
- SlotNode thisNode = fn as SlotNode;
- if (null == thisNode.getCorrectCardValue())
- {
- if (null != thisNode.getCardValue())
- {
- return false;
- }
- }
- else if (null == thisNode.getCardValue() || thisNode.getCardValue().getString() != thisNode.getCorrectCardValue().getString())
- {
- return false;
- }
- }
- fn = fn.getNext();
- }
- return true;
- }
- public void print()
- {
- FunctionNode fn = func;
- Console.WriteLine((int)ftype);
- while (null != fn)
- {
- if (fn is StringNode)
- {
- Console.Write((fn as StringNode).getString());
- }
- else
- {
- if ((fn as SlotNode).getCardValue() != null)
- {
- //Console.Write("(" + (fn as SlotNode).getCardValue().getString() +")");
- }
- else
- {
- Console.Write("[ SLOT ]");
- }
- }
- fn = fn.getNext();
- }
- }
- FunctionType ftype;
- public enum FunctionType
- {
- Func_F,
- Func_G,
- Func_H,
- Func_S,
- Func_P,
- };
- FunctionNode func;
- }
- class FunctionNode
- {
- public FunctionNode getNext()
- {
- return next;
- }
- public static FunctionNode fromString(string value)
- {
- string str = "";
- FunctionNode startOfList = null;
- FunctionNode endOfList = null;
- for (int i = 0; i < value.Length; ++i)
- {
- if (value[i] == '@')
- {
- SlotNode slot = new SlotNode();
- if (startOfList == null)
- {
- if (0 != str.CompareTo(""))
- {
- startOfList = new StringNode(str);
- endOfList = new SlotNode();
- startOfList.next = endOfList;
- endOfList.next = null;
- }
- else
- {
- startOfList = new SlotNode();
- endOfList = startOfList;
- endOfList.next = null;
- }
- }
- else
- {
- if (0 != str.CompareTo(""))
- {
- endOfList.next = new StringNode(str);
- endOfList = endOfList.next;
- }
- endOfList.next = new SlotNode();
- endOfList = endOfList.next;
- }
- str = "";
- }
- else
- {
- str += value[i];
- }
- }
- if (endOfList == null)
- {
- endOfList = new StringNode(str);
- startOfList = endOfList;
- }
- else
- {
- if (0 != str.CompareTo(""))
- {
- endOfList.next = new StringNode(str);
- }
- }
- return startOfList;
- }
- FunctionNode next;
- }
- class StringNode : FunctionNode
- {
- public string getString()
- {
- return stringValue;
- }
- public StringNode(string value)
- {
- stringValue = value;
- }
- string stringValue;
- }
- class SlotNode : FunctionNode
- {
- Card cardValue;
- bool clearing = false;
- Card correctCardValue;
- public SlotNode()
- {
- cardValue = new Card("x");
- cardValue.MouseUp += new MouseEventHandler(cardValue_MouseUp);
- //this.MouseUp += new MouseEventHandler(SlotNode_MouseUp);
- cardValue.MouseDown += new MouseEventHandler(SlotNode_MouseDown);
- cardValue.MouseUp += new MouseEventHandler(SlotNode_MouseUp);
- }
- void cardValue_MouseUp(object sender, MouseEventArgs e)
- {
- setCard(Fogger.it().getDraggedCard());
- }
- void SlotNode_MouseDown(object sender, MouseEventArgs e)
- {
- clearing = true;
- }
- void SlotNode_MouseUp(object sender, MouseEventArgs e)
- {
- if (clearing && !cardValue.ClientRectangle.Contains(e.Location))
- {
- setCard(null);
- }
- clearing = false;
- }
- public Card getCorrectCardValue()
- {
- return correctCardValue;
- }
- public Card getCardValue()
- {
- return cardValue;
- }
- public void setCorrectCard(Card card)
- {
- correctCardValue = card;
- }
- public void setCard(Card card)
- {
- if ( null != card)
- {
- cardValue.setString(card.getString());
- }
- else
- {
- cardValue.setString("x");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement