(* Content-type: application/mathematica *) (*** Wolfram Notebook File ***) (* http://www.wolfram.com/nb *) (* CreatedBy='Mathematica 6.0' *) (*CacheID: 234*) (* Internal cache information: NotebookFileLineBreakTest NotebookFileLineBreakTest NotebookDataPosition[ 145, 7] NotebookDataLength[ 34933, 986] NotebookOptionsPosition[ 32485, 902] NotebookOutlinePosition[ 33105, 926] CellTagsIndexPosition[ 33062, 923] WindowFrame->Normal*) (* Beginning of Notebook Content *) Notebook[{ Cell[TextData[{ "Creating user interfaces in ", StyleBox["Mathematica", FontSlant->"Italic"] }], "Title", CellChangeTimes->{{3.3907458272729015`*^9, 3.390745835374874*^9}, { 3.390746922311163*^9, 3.3907469286805754`*^9}, {3.390747109577908*^9, 3.390747121535579*^9}, {3.3949919891510243`*^9, 3.3949920025093*^9}, { 3.395346125984375*^9, 3.39534614046875*^9}, {3.3954160997502937`*^9, 3.3954160998804817`*^9}, {3.3954170292761545`*^9, 3.3954170434566865`*^9}}], Cell["\<\ Comp 160 Course Module 6 \ \>", "Subtitle", CellChangeTimes->{{3.390745838950158*^9, 3.3907458395911055`*^9}, 3.3907469187659235`*^9, {3.394991909622219*^9, 3.394991911955411*^9}, 3.3949920154269733`*^9, {3.395346122265625*^9, 3.395346122453125*^9}, 3.3954170235578747`*^9, {3.4271298722984543`*^9, 3.4271298724987507`*^9}, { 3.45961379515625*^9, 3.459613798015625*^9}}], Cell[TextData[{ "One of most important components of a game is its user interface. Many \ good game concepts are hampered by an inadequate user interface. In this \ model, we will work on improving the look and feel of ", StyleBox["Disasteroids", FontSlant->"Italic"], " by using ", StyleBox["Mathematica", FontSlant->"Italic"], "'s built-in user interface functions." }], "Text", CellChangeTimes->{{3.394992012613124*^9, 3.3949921192690425`*^9}, { 3.395346151296875*^9, 3.395346266640625*^9}, {3.3954112081877975`*^9, 3.3954112189633994`*^9}, {3.3954161134100714`*^9, 3.3954161204402504`*^9}, 3.3954165079413137`*^9, {3.3954173283091245`*^9, 3.3954174277330804`*^9}, { 3.3954962908229303`*^9, 3.3954963027503185`*^9}, 3.459613807234375*^9}], Cell[CellGroupData[{ Cell[TextData[{ "The current state of ", StyleBox["Disasteroids", FontSlant->"Italic"] }], "Section", CellChangeTimes->{{3.394384127590186*^9, 3.3943841323971944`*^9}, { 3.3943860065394526`*^9, 3.394386016584097*^9}, {3.3943862806590843`*^9, 3.3943862835232596`*^9}, {3.3944627701709642`*^9, 3.3944627761697097`*^9}, {3.39534637065625*^9, 3.39534638284375*^9}, 3.395358350984375*^9, {3.3954171137785053`*^9, 3.395417121559772*^9}}], Cell[TextData[{ "Since we will spend several modules working on ", StyleBox["Disasteroids", FontSlant->"Italic"], ", we will start with a clean version from the last module. Our \ implementation consisted of a background and a ship whose position was \ parameterized by a position and orientation." }], "Text", CellChangeTimes->{{3.3954174417233367`*^9, 3.395417535108549*^9}, { 3.399123171228747*^9, 3.3991232004310293`*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"background", "=", RowBox[{"Rectangle", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"-", "1"}], ",", RowBox[{"-", "1"}]}], "}"}], ",", RowBox[{"{", RowBox[{"1", ",", "1"}], "}"}]}], "]"}]}], ";"}], "\n", RowBox[{ RowBox[{"shipModel", "=", RowBox[{"Polygon", "[", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"1", ",", "0"}], "}"}], ",", RowBox[{"{", RowBox[{ RowBox[{"-", "1"}], ",", FractionBox["1", "2"]}], "}"}], ",", RowBox[{"{", RowBox[{ RowBox[{"-", "1"}], ",", FractionBox[ RowBox[{"-", "1"}], "2"]}], "}"}]}], "}"}], "]"}]}], ";"}], "\n", RowBox[{ RowBox[{"ship", "[", RowBox[{"p_", ",", "\[Theta]_"}], "]"}], ":=", RowBox[{"Translate", "[", RowBox[{ RowBox[{"Rotate", "[", RowBox[{ RowBox[{"Scale", "[", RowBox[{"shipModel", ",", FractionBox["1", "10"]}], "]"}], ",", "\[Theta]"}], "]"}], ",", "p"}], "]"}]}]}], "Input", CellChangeTimes->{{3.394462114505088*^9, 3.3944621175595407`*^9}, { 3.394462182233827*^9, 3.3944622029840784`*^9}, {3.394559454232637*^9, 3.3945594679125805`*^9}, {3.3954154048541527`*^9, 3.395415411063143*^9}, { 3.395417155168434*^9, 3.3954171685077477`*^9}, {3.395683744874175*^9, 3.395683753116108*^9}}], Cell[TextData[{ "We then used these components to create ", Cell[BoxData["DynamicModule"]], " that allowed interactive control of the ship's position and orientation." }], "Text", CellChangeTimes->{{3.3954175427796564`*^9, 3.395417584550136*^9}}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"DynamicModule", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"p", "=", RowBox[{"{", RowBox[{"0", ",", "0"}], "}"}]}], ",", RowBox[{"\[Theta]", "=", "0"}]}], "}"}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{ RowBox[{"Graphics", "[", RowBox[{"{", "\[IndentingNewLine]", RowBox[{ "background", ",", "\[IndentingNewLine]", "White", ",", "\[IndentingNewLine]", RowBox[{"Dynamic", "[", RowBox[{"ship", "[", RowBox[{"p", ",", "\[Theta]"}], "]"}], "]"}]}], "}"}], "]"}], ",", "\[IndentingNewLine]", RowBox[{"Slider2D", "[", RowBox[{ RowBox[{"Dynamic", "[", "p", "]"}], ",", RowBox[{"{", RowBox[{ RowBox[{"-", "1"}], ",", "1"}], "}"}]}], "]"}], ",", "\[IndentingNewLine]", RowBox[{"Slider", "[", RowBox[{ RowBox[{"Dynamic", "[", "\[Theta]", "]"}], ",", RowBox[{"{", RowBox[{"0", ",", RowBox[{"2", "\[Pi]"}]}], "}"}]}], "]"}]}], "}"}]}], "]"}]], "Input", CellChangeTimes->{{3.3944607240579944`*^9, 3.3944608783529367`*^9}, { 3.394460909508357*^9, 3.39446105302759*^9}, {3.3944611755261765`*^9, 3.3944612086544733`*^9}, {3.394462148164158*^9, 3.3944621541428747`*^9}, { 3.394462278434075*^9, 3.3944622799062214`*^9}, {3.394559335209116*^9, 3.394559378642436*^9}, {3.394559597321242*^9, 3.39455961138174*^9}, { 3.3945596512098045`*^9, 3.3945597333795977`*^9}, {3.394559828888838*^9, 3.3945598424886646`*^9}, {3.3945598777100124`*^9, 3.394559881135006*^9}, { 3.395416015157812*^9, 3.395416017741553*^9}, {3.395683758313634*^9, 3.3956837606770554`*^9}}], Cell[BoxData[ DynamicModuleBox[{$CellContext`p$$ = {0, 0}, $CellContext`\[Theta]$$ = 0}, RowBox[{"{", RowBox[{ GraphicsBox[{RectangleBox[{-1, -1}, {1, 1}], {GrayLevel[1], DynamicBox[Typeset`ToBoxes[ $CellContext`ship[$CellContext`p$$, $CellContext`\[Theta]$$], StandardForm, Graphics], ImageSizeCache->{{121.54999999999983`, 147.4499999999998}, {-12.500000000000025`, 0.49999999999997413`}}]}}, ImageCache->GraphicsData["CompressedBitmap", "\<\ eJzt2bFqg1AUxvFjTI0UkpJAdh/D0al1yUNICCRDaDGSSgutpRQpeQgfw9HR x3DLKzie5kpSgtzAIRRSwid4lB/37x3EyUkQzWfLIFpMA+chDJ7mi+nKuX8M d2QaRMZAnQ6peyY6DCbebu/UUHL+vaUurJ5pqmFBoVAoFAqFQqEX0EgB3XBV VVyWJadpyr7vs23b9K1fbDaL9wd1mtltx1+S1tK3H5L2tpkmF0XBSZKw53n0 JgkHmvBFEo404VoSjo/CXdSsWEnCoWbHZ0nY14SvktDev5G6rjnP80P8Lmm7 rTaOY3Zd91Pf9o7bdkPpqQ2zLPtdtPmPHxQUCoVCoVAo9Or1r34RkPED99kq EQ==\ \>"]], ",", Slider2DBox[Dynamic[$CellContext`p$$], {-1, 1}], ",", SliderBox[Dynamic[ BoxForm`RemapVariable[$CellContext`\[Theta]$$, {0, 2 Pi}], BoxForm`RemapValue[#, $CellContext`\[Theta]$$, {0, 2 Pi}]& ], {0, 1}]}], "}"}], DynamicModuleValues:>{}]], "Output", CellChangeTimes->{ 3.3945650844138823`*^9, 3.3945656950441027`*^9, 3.394565802560848*^9, 3.3948080721503906`*^9, 3.3948082425070505`*^9, 3.3948087399873524`*^9, 3.394809019151554*^9, 3.394813407415324*^9, 3.394919478796875*^9, { 3.395415769722446*^9, 3.395415791984679*^9}, 3.395415822218455*^9, 3.3954158609545407`*^9, 3.39541601960425*^9, 3.3954171783920593`*^9, 3.3954963222988176`*^9, 3.3956837635612316`*^9}] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[TextData[{ "Creating ", Cell[BoxData["Panels"]], " in ", StyleBox["Mathematica", FontSlant->"Italic"] }], "Section", CellChangeTimes->{{3.394305897149406*^9, 3.394305954022887*^9}, { 3.3943757782379036`*^9, 3.394375787381233*^9}, {3.394379052861884*^9, 3.3943790618750243`*^9}, {3.3944627806262064`*^9, 3.3944627815575647`*^9}, { 3.394557288915883*^9, 3.394557295805928*^9}, {3.3954171889273133`*^9, 3.3954172249394555`*^9}}], Cell[TextData[{ "One obvious problem with our current version of ", StyleBox["Disasteroids", FontSlant->"Italic"], " is that the game display and two sliders are joined using a list. To say \ that this arrangement is crude is an understatement. ", StyleBox["Mathematica", FontSlant->"Italic"], " support creation of a more visually appealing arrangement of game displays \ and control elements using ", Cell[BoxData["Panel"]], ". Returning to our initial example of ", Cell[BoxData["Dynamic"]], ", here is an example of using ", Cell[BoxData["Panel"]], " to spiff up the display. " }], "Text", CellChangeTimes->{{3.3954175995819006`*^9, 3.395417643004772*^9}, { 3.395417690082937*^9, 3.3954177770588694`*^9}, {3.39541782581947*^9, 3.3954178606799445`*^9}}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"DynamicModule", "[", RowBox[{ RowBox[{"{", RowBox[{"r", "=", "1"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{"Panel", "[", RowBox[{"{", RowBox[{ RowBox[{"Graphics", "[", RowBox[{"{", RowBox[{ RowBox[{"Rectangle", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"-", "1"}], ",", RowBox[{"-", "1"}]}], "}"}], ",", RowBox[{"{", RowBox[{"1", ",", "1"}], "}"}]}], "]"}], ",", "Gray", ",", RowBox[{"Disk", "[", RowBox[{ RowBox[{"{", RowBox[{"0", ",", "0"}], "}"}], ",", RowBox[{"Dynamic", "[", "r", " ", "]"}]}], "]"}]}], "}"}], "]"}], ",", "\[IndentingNewLine]", RowBox[{"Slider", "[", RowBox[{"Dynamic", "[", "r", "]"}], "]"}]}], "}"}], "]"}]}], "]"}]], "Input", CellChangeTimes->{{3.3954179017794523`*^9, 3.3954179092702985`*^9}}], Cell[BoxData[ DynamicModuleBox[{$CellContext`r$$ = 1.}, PanelBox[ RowBox[{"{", RowBox[{ GraphicsBox[{RectangleBox[{-1, -1}, {1, 1}], {GrayLevel[0.5], DiskBox[{0, 0}]}}], ",", SliderBox[Dynamic[$CellContext`r$$]]}], "}"}]], DynamicModuleValues:>{}]], "Output", CellChangeTimes->{3.395417909921241*^9, 3.395496338061798*^9}] }, Open ]], Cell[TextData[{ "What did ", Cell[BoxData["Panel"]], " do? It added a gray background box around the whole expression. \ Unfortunately, we still can see the list. To dispose of the list, we can use \ three other functions in conjunction with ", Cell[BoxData["Panel"]], " to organize its contents. These functions are ", Cell[BoxData["Column"]], ", ", Cell[BoxData["Row"]], " and ", Cell[BoxData["Grid"]], ". Used with ", Cell[BoxData["Panel"]], ", these functions yield a much nicer appearance." }], "Text", CellChangeTimes->{{3.3954179203863935`*^9, 3.39541807507036*^9}, 3.395418496440462*^9}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"DynamicModule", "[", RowBox[{ RowBox[{"{", RowBox[{"r", "=", "1"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{"Panel", "[", RowBox[{"Column", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"Graphics", "[", RowBox[{"{", RowBox[{ RowBox[{"Rectangle", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"-", "1"}], ",", RowBox[{"-", "1"}]}], "}"}], ",", RowBox[{"{", RowBox[{"1", ",", "1"}], "}"}]}], "]"}], ",", "Gray", ",", RowBox[{"Disk", "[", RowBox[{ RowBox[{"{", RowBox[{"0", ",", "0"}], "}"}], ",", RowBox[{"Dynamic", "[", "r", " ", "]"}]}], "]"}]}], "}"}], "]"}], ",", "\[IndentingNewLine]", RowBox[{"Slider", "[", RowBox[{"Dynamic", "[", "r", "]"}], "]"}]}], "}"}], ",", "Center"}], "]"}], "]"}]}], "]"}]], "Input", CellChangeTimes->{{3.3954179017794523`*^9, 3.3954179092702985`*^9}, { 3.3954180869876156`*^9, 3.395418091634343*^9}, {3.3954191070645857`*^9, 3.3954191081862097`*^9}}], Cell[BoxData[ DynamicModuleBox[{$CellContext`r$$ = 1.}, PanelBox[ TagBox[GridBox[{ { GraphicsBox[{RectangleBox[{-1, -1}, {1, 1}], {GrayLevel[0.5], DiskBox[{0, 0}]}}]}, { SliderBox[Dynamic[$CellContext`r$$]]} }, ColumnsEqual->False, GridBoxAlignment->{"Columns" -> {{Center}}}, GridBoxItemSize->{"Columns" -> {{Automatic}}, "Rows" -> {{Automatic}}}, RowsEqual->False], "Column"]], DynamicModuleValues:>{}]], "Output", CellChangeTimes->{3.395418092215184*^9, 3.39541910856676*^9, 3.3954963519220047`*^9}] }, Open ]], Cell[CellGroupData[{ Cell["Exercise", "Exercise", CellChangeTimes->{{3.395418510200385*^9, 3.3954185116324587`*^9}}], Cell[TextData[{ StyleBox["Modify the current definition of ", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["Disasteroids", FontSlant->"Italic", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" to add a ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["Panel"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[". Next, use ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["Column"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" to stack the background over a row consisting of the both \ sliders.", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.395418517210535*^9, 3.3954185198543634`*^9}, { 3.395418560903799*^9, 3.3954186189778843`*^9}}] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Adding text to our user interface", "Section", CellChangeTimes->{{3.395418367643978*^9, 3.395418369787081*^9}, { 3.3954186986131887`*^9, 3.3954187041011343`*^9}}], Cell[TextData[{ "One nice feature of ", StyleBox["Mathematica", FontSlant->"Italic"], " is that it treats text as just another type of graphical object. For \ example, we can the textual tag Size before the slider in our disk example by \ simply inserting the string \"Size\" in the appropriate place and using ", Cell[BoxData["Row"]], "." }], "Text", CellChangeTimes->{{3.3954181556470275`*^9, 3.3954181752353897`*^9}, { 3.395418760432697*^9, 3.3954188951076927`*^9}}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"DynamicModule", "[", RowBox[{ RowBox[{"{", RowBox[{"r", "=", "1"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{"Panel", "[", RowBox[{"Column", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"Graphics", "[", RowBox[{"{", RowBox[{ RowBox[{"Rectangle", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"-", "1"}], ",", RowBox[{"-", "1"}]}], "}"}], ",", RowBox[{"{", RowBox[{"1", ",", "1"}], "}"}]}], "]"}], ",", "Gray", ",", RowBox[{"Disk", "[", RowBox[{ RowBox[{"{", RowBox[{"0", ",", "0"}], "}"}], ",", RowBox[{"Dynamic", "[", "r", " ", "]"}]}], "]"}]}], "}"}], "]"}], ",", "\[IndentingNewLine]", RowBox[{"Row", "[", RowBox[{"{", RowBox[{"\"\\"", ",", RowBox[{"Slider", "[", RowBox[{"Dynamic", "[", "r", "]"}], "]"}]}], "}"}], "]"}]}], "}"}], ",", "Center"}], "]"}], "]"}]}], "]"}]], "Input", CellChangeTimes->{{3.395418187713456*^9, 3.3954182761014338`*^9}, { 3.395418321146654*^9, 3.3954183303399653`*^9}, {3.39541864881108*^9, 3.3954186873168325`*^9}, {3.3954187931200247`*^9, 3.3954188016623936`*^9}, { 3.3954191261321936`*^9, 3.3954191272938757`*^9}}], Cell[BoxData[ DynamicModuleBox[{$CellContext`r$$ = 1}, PanelBox[ TagBox[GridBox[{ { GraphicsBox[{RectangleBox[{-1, -1}, {1, 1}], {GrayLevel[0.5], DiskBox[{0, 0}]}}]}, { InterpretationBox[ RowBox[{"\<\"Size \"\>", "\[InvisibleSpace]", SliderBox[Dynamic[$CellContext`r$$]]}], Row[{"Size ", Slider[ Dynamic[$CellContext`r$$]]}]]} }, ColumnsEqual->False, GridBoxAlignment->{"Columns" -> {{Center}}}, GridBoxItemSize->{"Columns" -> {{Automatic}}, "Rows" -> {{Automatic}}}, RowsEqual->False], "Column"]], DynamicModuleValues:>{}]], "Output", CellChangeTimes->{{3.395418204017062*^9, 3.395418276572115*^9}, { 3.395418325783368*^9, 3.3954183308607197`*^9}, 3.3954186890693703`*^9, { 3.395418795443389*^9, 3.395418802794032*^9}, 3.3954191277345133`*^9}] }, Open ]], Cell[TextData[{ "If we are unhappy with the size, style or color of the text, we can wrap \ the string in the function ", Cell[BoxData["Style"]], " and add options. For example, we can increase the size of the text to \ font size 24 and color it ", Cell[BoxData["Green"]], " as follows." }], "Text", CellChangeTimes->{{3.394217816338971*^9, 3.394217828076082*^9}, { 3.394222857638538*^9, 3.3942228626558523`*^9}, {3.394286427438229*^9, 3.394286433617052*^9}, {3.394286512479665*^9, 3.394286520931734*^9}, { 3.3943062707277603`*^9, 3.3943062758753157`*^9}, {3.394379068124135*^9, 3.394379068124135*^9}, {3.3954189047516565`*^9, 3.3954189095586166`*^9}, { 3.3954189526910677`*^9, 3.3954190087221956`*^9}, {3.3954190434224377`*^9, 3.395419048629978*^9}, {3.395419155684983*^9, 3.3954191565362153`*^9}}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"DynamicModule", "[", RowBox[{ RowBox[{"{", RowBox[{"r", "=", "1"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{"Panel", "[", RowBox[{"Column", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"Graphics", "[", RowBox[{"{", RowBox[{ RowBox[{"Rectangle", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"-", "1"}], ",", RowBox[{"-", "1"}]}], "}"}], ",", RowBox[{"{", RowBox[{"1", ",", "1"}], "}"}]}], "]"}], ",", "Gray", ",", RowBox[{"Disk", "[", RowBox[{ RowBox[{"{", RowBox[{"0", ",", "0"}], "}"}], ",", RowBox[{"Dynamic", "[", "r", " ", "]"}]}], "]"}]}], "}"}], "]"}], ",", "\[IndentingNewLine]", RowBox[{"Row", "[", RowBox[{"{", RowBox[{ RowBox[{"Style", "[", RowBox[{"\"\\"", ",", "24", ",", "Green"}], "]"}], ",", RowBox[{"Slider", "[", RowBox[{"Dynamic", "[", "r", "]"}], "]"}]}], "}"}], "]"}]}], "}"}], ",", "Center"}], "]"}], "]"}]}], "]"}]], "Input", CellChangeTimes->{{3.395418187713456*^9, 3.3954182761014338`*^9}, { 3.395418321146654*^9, 3.3954183303399653`*^9}, {3.39541864881108*^9, 3.3954186873168325`*^9}, {3.3954187931200247`*^9, 3.3954188016623936`*^9}, { 3.3954189186017103`*^9, 3.3954189358967514`*^9}, {3.3954190190371304`*^9, 3.395419057472781*^9}, {3.3954191358863163`*^9, 3.3954191599010873`*^9}, { 3.460198327046875*^9, 3.460198331140625*^9}}], Cell[BoxData[ DynamicModuleBox[{$CellContext`r$$ = 1}, PanelBox[ TagBox[GridBox[{ { GraphicsBox[{RectangleBox[{-1, -1}, {1, 1}], {GrayLevel[0.5], DiskBox[{0, 0}]}}]}, { InterpretationBox[ RowBox[{ StyleBox["\<\"Size \"\>", StripOnInput->False, FrontFaceColor->RGBColor[0, 1, 0], BackFaceColor->RGBColor[0, 1, 0], GraphicsColor->RGBColor[0, 1, 0], FontSize->24, FontColor->RGBColor[0, 1, 0]], "\[InvisibleSpace]", SliderBox[Dynamic[$CellContext`r$$]]}], Row[{ Style[ "Size ", FontSize -> 24, $CellContext`Color -> RGBColor[0, 1, 0]], Slider[ Dynamic[$CellContext`r$$]]}]]} }, ColumnsEqual->False, GridBoxAlignment->{"Columns" -> {{Center}}}, GridBoxItemSize->{"Columns" -> {{Automatic}}, "Rows" -> {{Automatic}}}, RowsEqual->False], "Column"]], DynamicModuleValues:>{}]], "Output", CellChangeTimes->{{3.3954189296877613`*^9, 3.3954189371485643`*^9}, { 3.395419025055845*^9, 3.3954190583340282`*^9}, {3.3954191390108404`*^9, 3.3954191605320005`*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Exercise", "Exercise", CellChangeTimes->{{3.395418510200385*^9, 3.3954185116324587`*^9}}], Cell[TextData[{ StyleBox["Modify the current version of ", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["Disasteroids", FontSlant->"Italic", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" to include the title ", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["Disasteroids", FontSlant->"Italic", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" in red, 36 point, italics.", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.395418517210535*^9, 3.3954185198543634`*^9}, { 3.395418560903799*^9, 3.3954186189778843`*^9}, {3.3954187337039967`*^9, 3.3954187482450504`*^9}, {3.3954191516491394`*^9, 3.3954192091223545`*^9}}] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[TextData[{ "Adding ", Cell[BoxData["Buttons"]], " and other control elements" }], "Section", CellChangeTimes->{{3.3954172104584885`*^9, 3.395417221023786*^9}, { 3.3954196949357643`*^9, 3.395419697289172*^9}, {3.395419740511754*^9, 3.395419741633378*^9}}], Cell[TextData[{ "Luckily for us, ", StyleBox["Mathematica", FontSlant->"Italic"], " supports many other types of control elements for use in user interfaces. \ I suggest that you briefly review the section ", ButtonBox["Control Objects ", BaseStyle->"Link", ButtonData->"paclet:guide/ControlObjects"], "to gain a working knowledge of some of the other elements that are \ available. In this section, we will modify ", StyleBox["Disasteroids", FontSlant->"Italic"], " to use a different control model based on buttons instead of sliders. " }], "Text", CellChangeTimes->{{3.394460666484059*^9, 3.3944607063521814`*^9}, { 3.3944614648079123`*^9, 3.3944614738010235`*^9}, {3.395419745559062*^9, 3.3954199066623235`*^9}}], Cell[TextData[{ "To illustrate the interaction of these control objects with ", Cell[BoxData["Dynamic"]], ", I have replaced the ", Cell[BoxData["Slider"]], " in our disk example with a ", Cell[BoxData["RadioButtonBar"]], ". Try replace ", Cell[BoxData[ RowBox[{"Dynamic", "[", "r", "]"}]]], " by ", Cell[BoxData["r"]], " in the ", Cell[BoxData["RadioButtonBar"]], " definition and see what happens." }], "Text", CellChangeTimes->{{3.3954977608760786`*^9, 3.395497852880209*^9}}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"DynamicModule", "[", RowBox[{ RowBox[{"{", RowBox[{"r", "=", "1"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{"Panel", "[", RowBox[{"Column", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"Graphics", "[", RowBox[{"{", RowBox[{ RowBox[{"Rectangle", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"-", "1"}], ",", RowBox[{"-", "1"}]}], "}"}], ",", RowBox[{"{", RowBox[{"1", ",", "1"}], "}"}]}], "]"}], ",", "Gray", ",", RowBox[{"Disk", "[", RowBox[{ RowBox[{"{", RowBox[{"0", ",", "0"}], "}"}], ",", RowBox[{"Dynamic", "[", "r", " ", "]"}]}], "]"}]}], "}"}], "]"}], ",", "\[IndentingNewLine]", RowBox[{"RadioButtonBar", "[", RowBox[{ RowBox[{"Dynamic", "[", "r", "]"}], ",", RowBox[{"{", RowBox[{"0", ",", FractionBox["1", "3"], ",", FractionBox["2", "3"], ",", "1"}], "}"}]}], "]"}]}], "}"}], ",", "Center"}], "]"}], "]"}]}], "]"}]], "Input", CellChangeTimes->{{3.395418187713456*^9, 3.3954182761014338`*^9}, { 3.395418321146654*^9, 3.3954183303399653`*^9}, {3.39541864881108*^9, 3.3954186873168325`*^9}, {3.3954187931200247`*^9, 3.3954188016623936`*^9}, { 3.3954191261321936`*^9, 3.3954191272938757`*^9}, {3.395497655001727*^9, 3.395497750240573*^9}, {3.395498079120037*^9, 3.3954981246964817`*^9}}], Cell[BoxData[ DynamicModuleBox[{$CellContext`r$$ = Rational[2, 3]}, PanelBox[ TagBox[GridBox[{ { GraphicsBox[{RectangleBox[{-1, -1}, {1, 1}], {GrayLevel[0.5], DiskBox[{0, 0}, Dynamic[$CellContext`r$$]]}}]}, { InterpretationBox[ StyleBox[ RowBox[{ TagBox[GridBox[{ { RadioButtonBox[Dynamic[$CellContext`r$$], {0}], "0"} }, ColumnsEqual->False, GridBoxItemSize->{ "Columns" -> {{Automatic}}, "Rows" -> {{Automatic}}}, GridBoxSpacings->{"Columns" -> { Offset[0.27999999999999997`], { Offset[0.]}, Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> { Offset[0.2], { Offset[0.4]}, Offset[0.2]}, "RowsIndexed" -> {}}, RowsEqual->False], "Grid"], " ", TagBox[GridBox[{ { RadioButtonBox[Dynamic[$CellContext`r$$], {Rational[1, 3]}], FractionBox["1", "3"]} }, ColumnsEqual->False, GridBoxItemSize->{ "Columns" -> {{Automatic}}, "Rows" -> {{Automatic}}}, GridBoxSpacings->{"Columns" -> { Offset[0.27999999999999997`], { Offset[0.]}, Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> { Offset[0.2], { Offset[0.4]}, Offset[0.2]}, "RowsIndexed" -> {}}, RowsEqual->False], "Grid"], " ", TagBox[GridBox[{ { RadioButtonBox[Dynamic[$CellContext`r$$], {Rational[2, 3]}], FractionBox["2", "3"]} }, ColumnsEqual->False, GridBoxItemSize->{ "Columns" -> {{Automatic}}, "Rows" -> {{Automatic}}}, GridBoxSpacings->{"Columns" -> { Offset[0.27999999999999997`], { Offset[0.]}, Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> { Offset[0.2], { Offset[0.4]}, Offset[0.2]}, "RowsIndexed" -> {}}, RowsEqual->False], "Grid"], " ", TagBox[GridBox[{ { RadioButtonBox[Dynamic[$CellContext`r$$], {1}], "1"} }, ColumnsEqual->False, GridBoxItemSize->{ "Columns" -> {{Automatic}}, "Rows" -> {{Automatic}}}, GridBoxSpacings->{"Columns" -> { Offset[0.27999999999999997`], { Offset[0.]}, Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> { Offset[0.2], { Offset[0.4]}, Offset[0.2]}, "RowsIndexed" -> {}}, RowsEqual->False], "Grid"]}], Deployed->True], RadioButtonBar[ Dynamic[$CellContext`r$$], {0, Rational[1, 3], Rational[2, 3], 1}]]} }, ColumnsEqual->False, GridBoxAlignment->{"Columns" -> {{Center}}}, GridBoxItemSize->{"Columns" -> {{Automatic}}, "Rows" -> {{Automatic}}}, RowsEqual->False], "Column"]], DynamicModuleValues:>{}]], "Output", CellChangeTimes->{3.3954981252372704`*^9}] }, Open ]], Cell[TextData[{ "Using sliders, we were able to control the position ", Cell[BoxData["v"]], " and orientation ", Cell[BoxData["\[Theta]"]], " of the ship in absolute sense. The position of the sliders corresponded \ directly to position and orientation of the ship. In reality, the control \ systems for real physical objects such as cars, planes and spaceship do not \ allow direct control of the position and orientation of the object. Instead, \ the position and orientation of the objects is controlled incremental by \ applying a force to the object to change its velocity. Thus, typical control \ schemes for flying/racing games involve incremental updates to the position \ and orientation of an object. " }], "Text", CellChangeTimes->{{3.395420743103407*^9, 3.395420768780585*^9}, { 3.3954964027460995`*^9, 3.395496746637449*^9}, {3.395496975240723*^9, 3.3954969782751465`*^9}, 3.3954970451426315`*^9, {3.3954985880219507`*^9, 3.3954985890033817`*^9}}], Cell[CellGroupData[{ Cell["Exercises", "Exercise", CellChangeTimes->{{3.395418510200385*^9, 3.3954185116324587`*^9}, 3.3954979016613255`*^9}], Cell[TextData[{ StyleBox["1. Redo the disk example using both a ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["SetterBar"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" and an ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["InputField"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" to control the radius of the disk.", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.3954973724898615`*^9, 3.3954974453160324`*^9}, { 3.395497955950472*^9, 3.39549797114262*^9}, {3.39549813237768*^9, 3.3954981488617115`*^9}, {3.3954985603215675`*^9, 3.3954985771160517`*^9}}], Cell[TextData[{ StyleBox["2. For our next version of ", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["Disasteroids", FontSlant->"Italic", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[", I suggest that we adopt a control scheme based on four buttons: \ one that moves the ship forward, one that moves the ship backwards, one that \ rotates the ship counterclockwise, and one that rotates the ship clockwise. ", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["Mathematica", FontSlant->"Italic", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["'s function ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["Button"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" takes two arguments, a ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["label"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" and an ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["action"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[". The label is a string that will appear on the button and the \ action is a ", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["Mathematica", FontSlant->"Italic", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" expression that will be evaluated when the button is clicked. In \ the case of rotation buttons, the appropriate action is simple; increment or \ decrement ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["\[Theta]"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" slightly. For the forward and back buttons, we increment the \ position ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["v"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" by a small fraction of the forward vector ", FontColor->RGBColor[0.5, 0, 0.5]], Cell[BoxData["f"], FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" for the ship. ", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.395420743103407*^9, 3.395420768780585*^9}, { 3.3954964027460995`*^9, 3.395496746637449*^9}, {3.395496975240723*^9, 3.3954969782751465`*^9}, {3.3954970451426315`*^9, 3.395497340753594*^9}, { 3.3954979493007774`*^9, 3.3954979496713176`*^9}, {3.395498169882357*^9, 3.395498201889019*^9}}], Cell[TextData[{ StyleBox["Your task for this problem is to remove the two sliders in your \ old version of ", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox["Disasteroids", FontSlant->"Italic", FontColor->RGBColor[0.5, 0, 0.5]], StyleBox[" and instead add the four button control scheme described above.", FontColor->RGBColor[0.5, 0, 0.5]] }], "ExerciseText", CellChangeTimes->{{3.3954973724898615`*^9, 3.3954974453160324`*^9}, { 3.395498214777809*^9, 3.3954982212572556`*^9}}] }, Open ]] }, Open ]] }, WindowToolbars->{}, WindowSize->{1016, 949}, WindowMargins->{{0, Automatic}, {Automatic, 0}}, PrintingCopies->1, PrintingPageRange->{1, Automatic}, PrintingOptions->{"PrintCellBrackets"->False, "PrintMultipleHorizontalPages"->False, "PrintRegistrationMarks"->True, "PrintingMargins"->{{54, 54}, {72, 72}}}, ShowSelection->True, Magnification->1.5, FrontEndVersion->"7.0 for Microsoft Windows (32-bit) (November 10, 2008)", StyleDefinitions->"Classroom.nb" ] (* End of Notebook Content *) (* Internal cache information *) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[545, 20, 474, 9, 82, "Title"], Cell[1022, 31, 396, 8, 93, "Subtitle"], Cell[1421, 41, 766, 15, 84, "Text"], Cell[CellGroupData[{ Cell[2212, 60, 451, 9, 81, "Section"], Cell[2666, 71, 435, 9, 60, "Text"], Cell[3104, 82, 1387, 42, 180, "Input"], Cell[4494, 126, 250, 5, 36, "Text"], Cell[CellGroupData[{ Cell[4769, 135, 1707, 41, 232, "Input"], Cell[6479, 178, 1611, 32, 320, "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[8139, 216, 447, 11, 123, "Section"], Cell[8589, 229, 780, 19, 166, "Text"], Cell[CellGroupData[{ Cell[9394, 252, 977, 28, 226, "Input"], Cell[10374, 282, 356, 9, 526, "Output"] }, Open ]], Cell[10745, 294, 618, 18, 130, "Text"], Cell[CellGroupData[{ Cell[11388, 316, 1192, 32, 178, "Input"], Cell[12583, 350, 580, 17, 394, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[13200, 372, 96, 1, 56, "Exercise"], Cell[13299, 375, 691, 19, 61, "ExerciseText"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[14039, 400, 171, 2, 81, "Section"], Cell[14213, 404, 480, 11, 61, "Text"], Cell[CellGroupData[{ Cell[14718, 419, 1407, 36, 178, "Input"], Cell[16128, 457, 873, 23, 394, "Output"] }, Open ]], Cell[17016, 483, 821, 15, 62, "Text"], Cell[CellGroupData[{ Cell[17862, 502, 1646, 40, 178, "Input"], Cell[19511, 544, 1165, 32, 398, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[20713, 581, 96, 1, 56, "Exercise"], Cell[20812, 584, 660, 16, 35, "ExerciseText"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[21521, 606, 268, 7, 82, "Section"], Cell[21792, 615, 740, 17, 84, "Text"], Cell[22535, 634, 496, 16, 62, "Text"], Cell[CellGroupData[{ Cell[23056, 654, 1572, 39, 249, "Input"], Cell[24631, 695, 3419, 93, 588, "Output"] }, Open ]], Cell[28065, 791, 983, 17, 201, "Text"], Cell[CellGroupData[{ Cell[29073, 812, 125, 2, 84, "Exercise"], Cell[29201, 816, 618, 14, 54, "ExerciseText"], Cell[29822, 832, 2144, 53, 277, "ExerciseText"], Cell[31969, 887, 488, 11, 90, "ExerciseText"] }, Open ]] }, Open ]] } ] *) (* End of internal cache information *)