Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace Fogger2
  11. {
  12.     public partial class fogFunction : UserControl
  13.     {
  14.         private string rawString;
  15.         public fogFunction(string value, FunctionType type1)
  16.         {
  17.             InitializeComponent();
  18.             rawString = value;
  19.  
  20.             func = FunctionNode.fromString(value);
  21.             ftype = type1;
  22.  
  23.             FunctionNode fn = func;
  24.  
  25.             while (null != fn)
  26.             {
  27.                 if (fn is SlotNode)
  28.                 {
  29.                     SlotNode sn = fn as SlotNode;
  30.                     Controls.Add(sn.getCardValue());
  31.                 }
  32.                 fn = fn.getNext();
  33.             }
  34.             slotIndex = 0;
  35.             DrawMathString(CreateGraphics(), value, false, 16.0f, 32.0f);
  36.  
  37.             Paint += new PaintEventHandler(fogFunction_Paint);
  38.  
  39.         }
  40.  
  41.         void fogFunction_Paint(object sender, PaintEventArgs e)
  42.         {
  43.             slotIndex = 0;
  44.             DrawMathString(e.Graphics, rawString, true, 16.0f, 32.0f);
  45.  
  46.  
  47.  
  48.  
  49.  
  50.         }
  51.  
  52.         enum TokenType
  53.         {
  54.             TT_SQRT,
  55.             TT_POW,
  56.             TT_SLOT,
  57.         };
  58.  
  59.  
  60.         RectangleF merge(RectangleF l, RectangleF r)
  61.         {
  62.             float[] points =
  63.             { Math.Min( l.Left, r.Left )
  64.             , Math.Min( l.Top, r.Top )
  65.             , Math.Max( l.Right, r.Right )
  66.             , Math.Max( l.Bottom, r.Bottom )
  67.             };
  68.             return new RectangleF(points[0], points[1], points[2] - points[0], points[3] - points[1]);
  69.         }
  70.  
  71.         int slotIndex = 0;
  72.         RectangleF DrawMathString(Graphics g, string str, bool draw = true, float offset_x = 0.0f, float offset_y = 0.0f, float scale = 1.0f)
  73.         {
  74.             RectangleF coordsOut = new RectangleF(offset_x, offset_y, 0, 0);
  75.             Font f = new Font(Font.FontFamily, Font.Size * scale);
  76.  
  77.             float cursor_x = offset_x;
  78.             float cursor_y = offset_y;
  79.             int slotHeight = 50;
  80.             int slotWidth = 100;
  81.             int slotOffset = (slotHeight - Font.Height) / 2;
  82.  
  83.             for (int i = 0; i < str.Length; ++i)
  84.             {
  85.                 TokenType tt = TokenType.TT_POW;
  86.                 string this_draw = "";
  87.                 char c = str[i];
  88.                 switch (c)
  89.                 {
  90.                     case 's':
  91.                         if (str.Length - i < 4
  92.                         || str[i + 1] != 'q'
  93.                         || str[i + 2] != 'r'
  94.                         || str[i + 3] != 't')
  95.                         {
  96.                             this_draw += c;
  97.                             break;
  98.                         }
  99.                         else
  100.                         {
  101.                             i += 3;
  102.                             tt = TokenType.TT_SQRT;
  103.                         }
  104.                         goto case '^';
  105.                     case '^':
  106.                         i += 1;
  107.                         if (str[i] != '(')
  108.                         {
  109.                             MessageBox.Show("Error: ^ or sqrt must be proceeded by (");
  110.                         }
  111.                         else
  112.                         {
  113.                             int open_brackets = 1;
  114.                             int j = i + 1;
  115.                             for (j = i + 1; j < str.Length; ++j)
  116.                             {
  117.                                 if (str[j] == '(')
  118.                                     ++open_brackets;
  119.                                 else if (str[j] == ')')
  120.                                 {
  121.                                     if (0 == --open_brackets)
  122.                                         break;
  123.                                 }
  124.                             }
  125.                             if (0 != open_brackets)
  126.                             {
  127.                                 MessageBox.Show("Error: bracket was not correctly closed");
  128.                             }
  129.                             string subString = str.Substring(i + 1, j - i - 1);
  130.  
  131.                             if (tt == TokenType.TT_POW)
  132.                             {
  133.                                 float offset = Font.Height * 0.4f;
  134.                                 RectangleF bounds = DrawMathString(g, subString, draw, cursor_x, cursor_y - offset, scale * 0.8f);
  135.                                 coordsOut = merge(coordsOut, bounds);
  136.                                 cursor_x += bounds.Width;
  137.                             }
  138.                             else
  139.                             {
  140.                                 float sqrt_length = 14;
  141.                                 RectangleF bounds = DrawMathString(g, subString, draw, cursor_x + sqrt_length, cursor_y, scale);
  142.  
  143.                                 float top = bounds.Top - 4;
  144.                                 float bottom = bounds.Bottom + 4;
  145.                                 float flick_height = bottom - 8;
  146.                                 float first_x = cursor_x + 3;
  147.                                 float second_x = cursor_x + 6;
  148.                                 float third_x = bounds.Right + 2;
  149.  
  150.                                 if (draw)
  151.                                 {
  152.                                     g.DrawLine(SystemPens.ControlText, cursor_x, flick_height, first_x, bottom);
  153.                                     g.DrawLine(SystemPens.ControlText, first_x, bottom, second_x, top);
  154.                                     g.DrawLine(SystemPens.ControlText, second_x, top, third_x, top);
  155.                                 }
  156.  
  157.                                 bounds.X = cursor_x;
  158.                                 bounds.Width = third_x - cursor_x + 8;
  159.                                 bounds.Y = bottom;
  160.                                 bounds.Height += top - bottom;
  161.  
  162.                                 cursor_x += bounds.Width;
  163.                                 coordsOut = merge(coordsOut, bounds);
  164.  
  165.                             }
  166.                             i = j;
  167.                         }
  168.                         break;
  169.                     case '@':
  170.  
  171.                         RectangleF rect = new RectangleF(cursor_x, cursor_y - slotOffset, slotWidth, slotHeight);
  172.                         coordsOut = merge(coordsOut, rect);
  173.                         if (!draw)
  174.                         {
  175.                             Controls[slotIndex].SetBounds((int)cursor_x, (int)cursor_y - slotOffset, (int)slotWidth, (int)slotHeight);
  176.                         }
  177.                         ++slotIndex;
  178.  
  179.                         cursor_x += slotWidth;
  180.                         break;
  181.                     default:
  182.                         this_draw += c;
  183.                         break;
  184.                 }
  185.                 if (this_draw.Length > 0)
  186.                 {
  187.                     if (draw)
  188.                     {
  189.                         g.DrawString(this_draw, f, SystemBrushes.ControlText, cursor_x, cursor_y);
  190.                     }
  191.                     SizeF sz = g.MeasureString(this_draw, f);
  192.                     RectangleF rect = new RectangleF(cursor_x, cursor_y, sz.Width, sz.Height);
  193.                     coordsOut = merge(coordsOut, rect);
  194.                     cursor_x += sz.Width;
  195.                 }
  196.             }
  197.             return coordsOut;
  198.         }
  199.  
  200.         public void setCorrectCard(int slot, Card card)
  201.         {
  202.             FunctionNode currentNode = func;
  203.             int count = 0;
  204.             while (currentNode != null)
  205.             {
  206.                 if (currentNode is SlotNode)
  207.                 {
  208.                     count += 1;
  209.                     if (count == slot)
  210.                     {
  211.                         (currentNode as SlotNode).setCorrectCard(card);
  212.                     }
  213.  
  214.                 }
  215.                 currentNode = currentNode.getNext();
  216.             }
  217.  
  218.         }
  219.         public void setCard(int slot, Card card)
  220.         {
  221.             FunctionNode currentNode = func;
  222.             int count = 0;
  223.             while (currentNode != null)
  224.             {
  225.                 if (currentNode is SlotNode)
  226.                 {
  227.                     count += 1;
  228.                     if (count == slot)
  229.                     {
  230.                         (currentNode as SlotNode).setCard(card);
  231.                     }
  232.  
  233.                 }
  234.                 currentNode = currentNode.getNext();
  235.             }
  236.         }
  237.  
  238.  
  239.         public bool validateSolution()
  240.         {
  241.             FunctionNode fn = func;
  242.             while (null != fn)
  243.             {
  244.                 if (fn is SlotNode)
  245.                 {
  246.                     SlotNode thisNode = fn as SlotNode;
  247.                     if (null == thisNode.getCorrectCardValue())
  248.                     {
  249.                         if (null != thisNode.getCardValue())
  250.                         {
  251.  
  252.                             return false;
  253.                         }
  254.                     }
  255.  
  256.                     else if (null == thisNode.getCardValue() || thisNode.getCardValue().getString() != thisNode.getCorrectCardValue().getString())
  257.                     {
  258.                         return false;
  259.                     }
  260.                 }
  261.                 fn = fn.getNext();
  262.             }
  263.             return true;
  264.  
  265.         }
  266.  
  267.         public void print()
  268.         {
  269.  
  270.             FunctionNode fn = func;
  271.             Console.WriteLine((int)ftype);
  272.             while (null != fn)
  273.             {
  274.  
  275.                 if (fn is StringNode)
  276.                 {
  277.                     Console.Write((fn as StringNode).getString());
  278.                 }
  279.                 else
  280.                 {
  281.                     if ((fn as SlotNode).getCardValue() != null)
  282.                     {
  283.                         //Console.Write("(" + (fn as SlotNode).getCardValue().getString() +")");
  284.                     }
  285.                     else
  286.                     {
  287.                         Console.Write("[ SLOT ]");
  288.                     }
  289.                 }
  290.                 fn = fn.getNext();
  291.             }
  292.  
  293.  
  294.         }
  295.  
  296.  
  297.  
  298.  
  299.         FunctionType ftype;
  300.         public enum FunctionType
  301.         {
  302.             Func_F,
  303.             Func_G,
  304.             Func_H,
  305.             Func_S,
  306.             Func_P,
  307.         };
  308.  
  309.         FunctionNode func;
  310.     }
  311.  
  312.     class FunctionNode
  313.     {
  314.         public FunctionNode getNext()
  315.         {
  316.  
  317.             return next;
  318.         }
  319.  
  320.         public static FunctionNode fromString(string value)
  321.         {
  322.             string str = "";
  323.             FunctionNode startOfList = null;
  324.             FunctionNode endOfList = null;
  325.  
  326.             for (int i = 0; i < value.Length; ++i)
  327.             {
  328.                 if (value[i] == '@')
  329.                 {
  330.                     SlotNode slot = new SlotNode();
  331.  
  332.                     if (startOfList == null)
  333.                     {
  334.                         if (0 != str.CompareTo(""))
  335.                         {
  336.                             startOfList = new StringNode(str);
  337.                             endOfList = new SlotNode();
  338.                             startOfList.next = endOfList;
  339.                             endOfList.next = null;
  340.  
  341.                         }
  342.  
  343.                         else
  344.                         {
  345.  
  346.                             startOfList = new SlotNode();
  347.                             endOfList = startOfList;
  348.                             endOfList.next = null;
  349.  
  350.  
  351.  
  352.                         }
  353.                     }
  354.  
  355.  
  356.                     else
  357.                     {
  358.                         if (0 != str.CompareTo(""))
  359.                         {
  360.  
  361.                             endOfList.next = new StringNode(str);
  362.                             endOfList = endOfList.next;
  363.  
  364.  
  365.                         }
  366.                         endOfList.next = new SlotNode();
  367.                         endOfList = endOfList.next;
  368.                     }
  369.                     str = "";
  370.                 }
  371.  
  372.                 else
  373.                 {
  374.  
  375.                     str += value[i];
  376.                 }
  377.             }
  378.             if (endOfList == null)
  379.             {
  380.                 endOfList = new StringNode(str);
  381.                 startOfList = endOfList;
  382.             }
  383.             else
  384.             {
  385.                 if (0 != str.CompareTo(""))
  386.                 {
  387.                     endOfList.next = new StringNode(str);
  388.                 }
  389.             }
  390.             return startOfList;
  391.         }
  392.         FunctionNode next;
  393.     }
  394.  
  395.     class StringNode : FunctionNode
  396.     {
  397.  
  398.         public string getString()
  399.         {
  400.             return stringValue;
  401.         }
  402.  
  403.         public StringNode(string value)
  404.         {
  405.             stringValue = value;
  406.         }
  407.  
  408.         string stringValue;
  409.     }
  410.  
  411.     class SlotNode : FunctionNode
  412.     {
  413.  
  414.         Card cardValue;
  415.         bool clearing = false;
  416.         Card correctCardValue;
  417.  
  418.         public SlotNode()
  419.         {
  420.             cardValue = new Card("x");
  421.  
  422.             cardValue.MouseUp += new MouseEventHandler(cardValue_MouseUp);
  423.             //this.MouseUp += new MouseEventHandler(SlotNode_MouseUp);
  424.             cardValue.MouseDown += new MouseEventHandler(SlotNode_MouseDown);
  425.             cardValue.MouseUp += new MouseEventHandler(SlotNode_MouseUp);
  426.         }
  427.  
  428.         void cardValue_MouseUp(object sender, MouseEventArgs e)
  429.         {
  430.             setCard(Fogger.it().getDraggedCard());
  431.         }
  432.  
  433.         void SlotNode_MouseDown(object sender, MouseEventArgs e)
  434.         {
  435.             clearing = true;
  436.         }
  437.         void SlotNode_MouseUp(object sender, MouseEventArgs e)
  438.         {
  439.             if (clearing && !cardValue.ClientRectangle.Contains(e.Location))
  440.             {
  441.                 setCard(null);
  442.             }
  443.             clearing = false;
  444.         }
  445.         public Card getCorrectCardValue()
  446.         {
  447.             return correctCardValue;
  448.         }
  449.  
  450.         public Card getCardValue()
  451.         {
  452.             return cardValue;
  453.         }
  454.  
  455.         public void setCorrectCard(Card card)
  456.         {
  457.             correctCardValue = card;
  458.         }
  459.         public void setCard(Card card)
  460.         {
  461.             if ( null != card)
  462.             {
  463.                 cardValue.setString(card.getString());
  464.             }
  465.             else
  466.             {
  467.                 cardValue.setString("x");
  468.             }
  469.         }
  470.     }
  471.  
  472. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement