private void RandomizeGrid(GridPane grid) { for (int x, y = 0, i = 0; y < grid.GridHeight; ++y) { for (x = 0; x < grid.GridWidth; ++x, ++i) { ((grid.Get(x, y) as LayoutPane).Children[0] as MenuItem) .ImageItem.Color = RandomColor(); } } }
private void InitGrids() { gameOverText = GetItem <TextItem>("gameOverText"); gridPane = GetItem <StackPane>("gridPane"); var sgrid = GetItem <GridPane>("smallGrid"); var mgrid = GetItem <GridPane>("mediumGrid"); var lgrid = GetItem <GridPane>("largeGrid"); var grids = new GridPane[] { sgrid, mgrid, lgrid }; foreach (var grid in grids) { for (int x, y = 0, i = 0; y < grid.GridHeight; ++y) { for (x = 0; x < grid.GridWidth; ++x, ++i) { var cell = grid.Get(x, y) as StackPane; var mni = cell.Children[0] as MenuItem; var fader = cell.Children[1] as FadingPane; var complete = false; int index = i; mni.ImageItem.Color = RandomColor(); mni.Action += (s, a) => { if (fader.State != FadingPaneState.FadedIn) { return; } if (Session.IsListening) { complete = Session.Listen(index); fader.FadeOut(); } }; fader.FadedOut += (s, a) => fader.FadeIn(); fader.FadedIn += (s, a) => { if (complete) { Session.EvaluateNextTiles(); complete = false; } }; } } } }
private void CreateSession(Difficulty d, int t) { var gid = d == Difficulty.Easy ? "smallGrid": d == Difficulty.Medium ? "mediumGrid" : "largeGrid"; activeGrid = GetItem <GridPane>(gid); gridPane.Clear(); gridPane.Add(activeGrid); int w = activeGrid.GridWidth; int h = activeGrid.GridHeight; Session = new Session(w, h, t); for (int x, y = 0; y < h; ++y) { for (x = 0; x < w; ++x) { var fader = (activeGrid.Get(x, y) as LayoutPane).Children[1] as FadingPane; fader.FadeInTime = fader.FadeOutTime = ButtonFade; } } Session.NewIndex += (s, a) => { int y = a.Index / activeGrid.GridWidth; int x = a.Index % activeGrid.GridWidth; var fader = (activeGrid.Get(x, y) as StackPane).Children[1] as FadingPane; fader.FadeOut(); Console.WriteLine("Session says " + a.Index); }; Session.GameOver += (s, a) => { gameOverText.Text = string.Format("GameOver after {0} turn{1}", a.Turns, a.Turns == 1 ? "" : "s"); Console.WriteLine(string.Format("GameOver after {0} turn{1}", a.Turns, a.Turns == 1 ? "" : "s")); StopSession(true); }; }
//------------------------------------------------------------------------- public override void start(Stage primaryStage) { LocalDate today = LocalDate.now(ZoneId.systemDefault()); // setup GUI elements Label startLbl = new Label("Start date:"); DatePicker startInp = new DatePicker(today); startLbl.LabelFor = startInp; startInp.ShowWeekNumbers = false; Label endLbl = new Label("End date:"); DatePicker endInp = new DatePicker(today.plusYears(1)); endLbl.LabelFor = endInp; endInp.ShowWeekNumbers = false; Label freqLbl = new Label("Frequency:"); ChoiceBox <Frequency> freqInp = new ChoiceBox <Frequency>(FXCollections.observableArrayList(Frequency.P1M, Frequency.P2M, Frequency.P3M, Frequency.P4M, Frequency.P6M, Frequency.P12M)); freqLbl.LabelFor = freqInp; freqInp.Value = Frequency.P3M; Label stubLbl = new Label("Stub:"); ObservableList <StubConvention> stubOptions = FXCollections.observableArrayList(StubConvention.values()); stubOptions.add(0, null); ChoiceBox <StubConvention> stubInp = new ChoiceBox <StubConvention>(stubOptions); stubLbl.LabelFor = stubInp; stubInp.Value = StubConvention.SMART_INITIAL; Label rollLbl = new Label("Roll:"); ChoiceBox <RollConvention> rollInp = new ChoiceBox <RollConvention>(FXCollections.observableArrayList(null, RollConventions.NONE, RollConventions.EOM, RollConventions.IMM, RollConventions.IMMAUD, RollConventions.IMMNZD, RollConventions.SFE)); rollLbl.LabelFor = rollInp; rollInp.Value = RollConventions.NONE; Label bdcLbl = new Label("Adjust:"); ChoiceBox <BusinessDayConvention> bdcInp = new ChoiceBox <BusinessDayConvention>(FXCollections.observableArrayList(BusinessDayConventions.NO_ADJUST, BusinessDayConventions.FOLLOWING, BusinessDayConventions.MODIFIED_FOLLOWING, BusinessDayConventions.PRECEDING, BusinessDayConventions.MODIFIED_PRECEDING, BusinessDayConventions.MODIFIED_FOLLOWING_BI_MONTHLY, BusinessDayConventions.NEAREST)); bdcLbl.LabelFor = bdcInp; bdcInp.Value = BusinessDayConventions.MODIFIED_FOLLOWING; Label holidayLbl = new Label("Holidays:"); ChoiceBox <HolidayCalendarId> holidayInp = new ChoiceBox <HolidayCalendarId>(FXCollections.observableArrayList(HolidayCalendarIds.CHZU, HolidayCalendarIds.GBLO, HolidayCalendarIds.EUTA, HolidayCalendarIds.FRPA, HolidayCalendarIds.JPTO, HolidayCalendarIds.NYFD, HolidayCalendarIds.NYSE, HolidayCalendarIds.USNY, HolidayCalendarIds.USGS, HolidayCalendarIds.NO_HOLIDAYS, HolidayCalendarIds.SAT_SUN)); holidayLbl.LabelFor = holidayInp; holidayInp.Value = HolidayCalendarIds.GBLO; TableView <SchedulePeriod> resultGrid = new TableView <SchedulePeriod>(); TableColumn <SchedulePeriod, LocalDate> unadjustedCol = new TableColumn <SchedulePeriod, LocalDate>("Unadjusted dates"); TableColumn <SchedulePeriod, LocalDate> adjustedCol = new TableColumn <SchedulePeriod, LocalDate>("Adjusted dates"); TableColumn <SchedulePeriod, LocalDate> resultUnadjStartCol = new TableColumn <SchedulePeriod, LocalDate>("Start"); resultUnadjStartCol.CellValueFactory = new TableCallback <>(SchedulePeriod.meta().unadjustedStartDate()); TableColumn <SchedulePeriod, LocalDate> resultUnadjEndCol = new TableColumn <SchedulePeriod, LocalDate>("End"); resultUnadjEndCol.CellValueFactory = new TableCallback <>(SchedulePeriod.meta().unadjustedEndDate()); TableColumn <SchedulePeriod, Period> resultUnadjLenCol = new TableColumn <SchedulePeriod, Period>("Length"); resultUnadjLenCol.CellValueFactory = ReadOnlyCallback.of(sch => Period.between(sch.UnadjustedStartDate, sch.UnadjustedEndDate)); TableColumn <SchedulePeriod, LocalDate> resultStartCol = new TableColumn <SchedulePeriod, LocalDate>("Start"); resultStartCol.CellValueFactory = new TableCallback <>(SchedulePeriod.meta().startDate()); TableColumn <SchedulePeriod, LocalDate> resultEndCol = new TableColumn <SchedulePeriod, LocalDate>("End"); resultEndCol.CellValueFactory = new TableCallback <>(SchedulePeriod.meta().endDate()); TableColumn <SchedulePeriod, Period> resultLenCol = new TableColumn <SchedulePeriod, Period>("Length"); resultLenCol.CellValueFactory = ReadOnlyCallback.of(sch => sch.length()); unadjustedCol.Columns.add(resultUnadjStartCol); unadjustedCol.Columns.add(resultUnadjEndCol); unadjustedCol.Columns.add(resultUnadjLenCol); adjustedCol.Columns.add(resultStartCol); adjustedCol.Columns.add(resultEndCol); adjustedCol.Columns.add(resultLenCol); resultGrid.Columns.add(unadjustedCol); resultGrid.Columns.add(adjustedCol); resultGrid.Placeholder = new Label("Schedule not yet generated"); unadjustedCol.prefWidthProperty().bind(resultGrid.widthProperty().divide(2)); adjustedCol.prefWidthProperty().bind(resultGrid.widthProperty().divide(2)); resultUnadjStartCol.prefWidthProperty().bind(unadjustedCol.widthProperty().divide(3)); resultUnadjEndCol.prefWidthProperty().bind(unadjustedCol.widthProperty().divide(3)); resultUnadjLenCol.prefWidthProperty().bind(unadjustedCol.widthProperty().divide(3)); resultStartCol.prefWidthProperty().bind(adjustedCol.widthProperty().divide(3)); resultEndCol.prefWidthProperty().bind(adjustedCol.widthProperty().divide(3)); resultLenCol.prefWidthProperty().bind(adjustedCol.widthProperty().divide(3)); // setup generation button // this uses the GUI thread which is not the best idea Button btn = new Button(); btn.Text = "Generate"; btn.OnAction = @event => { LocalDate start = startInp.Value; LocalDate end = endInp.Value; Frequency freq = freqInp.Value; StubConvention stub = stubInp.Value; RollConvention roll = rollInp.Value; HolidayCalendarId holCal = holidayInp.Value; BusinessDayConvention bdc = bdcInp.Value; BusinessDayAdjustment bda = BusinessDayAdjustment.of(bdc, holCal); PeriodicSchedule defn = PeriodicSchedule.builder().startDate(start).endDate(end).frequency(freq).businessDayAdjustment(bda).stubConvention(stub).rollConvention(roll).build(); try { Schedule schedule = defn.createSchedule(REF_DATA); Console.WriteLine(schedule); resultGrid.Items = FXCollections.observableArrayList(schedule.Periods); } catch (ScheduleException ex) { resultGrid.Items = FXCollections.emptyObservableList(); resultGrid.Placeholder = new Label(ex.Message); Console.WriteLine(ex.Message); } }; // layout the components GridPane gp = new GridPane(); gp.Hgap = 10; gp.Vgap = 10; gp.Padding = new Insets(0, 10, 0, 10); gp.add(startLbl, 1, 1); gp.add(startInp, 2, 1); gp.add(endLbl, 1, 2); gp.add(endInp, 2, 2); gp.add(freqLbl, 1, 3); gp.add(freqInp, 2, 3); gp.add(bdcLbl, 3, 1); gp.add(bdcInp, 4, 1); gp.add(holidayLbl, 3, 2); gp.add(holidayInp, 4, 2); gp.add(stubLbl, 3, 3); gp.add(stubInp, 4, 3); gp.add(rollLbl, 3, 4); gp.add(rollInp, 4, 4); gp.add(btn, 3, 5, 2, 1); gp.add(resultGrid, 1, 7, 4, 1); BorderPane bp = new BorderPane(gp); Scene scene = new Scene(bp, 600, 600); // launch primaryStage.Title = "Periodic schedule generator"; primaryStage.Scene = scene; primaryStage.show(); }
protected override void Init() { // fill grids with random colored images var grid00 = GetItem <GridPane>("slp00.gdp"); var grid10 = GetItem <GridPane>("slp01.gdp"); var grid01 = GetItem <GridPane>("slp10.gdp"); var grid11 = GetItem <GridPane>("slp11.gdp"); var grids = new GridPane[] { grid00, grid10, grid01, grid11 }; foreach (var grid in grids) { for (int x, y = 0; y < gridHeight; ++y) { for (x = 0; x < gridWidth; ++x) { var img = Content.Load <Texture2D>("textures/blank"); var mni = new MenuItem(img); mni.ImageItem.KeepAspectRatio = true; mni.ImageItem.Color = RandomColor(); mni.HGrow = mni.VGrow = 1; InitMenuItemEvents(mni); grid.Set(x, y, mni); } } } // define slideshow behaviour var slp00 = GetItem <SlidingPane>("slp00"); var slp10 = GetItem <SlidingPane>("slp10"); var slp01 = GetItem <SlidingPane>("slp01"); var slp11 = GetItem <SlidingPane>("slp11"); List <SlidingPane> textSlider = GetItem <StackPane>("stpStory") .Children.Cast <SlidingPane>().ToList(); slp00.SlidedIn += (s, a) => slp10.SlideIn(); slp10.SlidedIn += (s, a) => slp01.SlideIn(); slp01.SlidedIn += (s, a) => slp11.SlideIn(); slp11.SlidedIn += (s, a) => { slp00.SlideOut(); slp10.SlideOut(); slp01.SlideOut(); slp11.SlideOut(); }; var fdpTroll = GetItem <FadingPane>("fdpTroll"); fdpTroll.FadedOut += (s, a) => slp00.SlideIn(); // restarts loop var slpQuit = textSlider[0]; slpQuit.SlidedIn += (s, a) => { // does nothing but prevents further drawing // TODO fix bug: lock position when slided in? slpQuit.SlideOut(slpQuit.GenericTarget); fdpTroll.FadeIn(); }; var mniQuit = GetItem <MenuItem>("mniQuit"); var oldColor = mniQuit.TextItem.Color; mniQuit.FocusGain += (s, a) => mniQuit.TextItem.Color = Color.Yellow; mniQuit.FocusLoss += (s, a) => mniQuit.TextItem.Color = oldColor; mniQuit.Action += (s, a) => Close(); if (textSlider.Count == 1) { return; } slp11.SlidedIn += (s, a) => textSlider[1].SlideIn(GenericPosition.Bottom); var rng = new Random(); var dirs = new GenericPosition[] { GenericPosition.Left, GenericPosition.Top, GenericPosition.Right, GenericPosition.Bottom }; for (int i = 1; i < textSlider.Count; ++i) { var curr = textSlider[i]; if (i == textSlider.Count - 1) { curr.SlidedOut += (s, a) => { int r = rng.Next(dirs.Length); var quitIn = dirs[r]; var quitOut = dirs[(r + dirs.Length / 2) % dirs.Length]; slpQuit.SlideIn(quitIn, quitOut); } } ; else { var succ = textSlider[i + 1]; curr.SlidedIn += (s, a) => succ.SlideIn(GenericPosition.Bottom); } curr.SlidedIn += (s, a) => curr.SlideOut(dirs[rng.Next(dirs.Length - 1)]); } }