1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
package my3d; import java.util.ArrayList; import javafx.application.Application; import javafx.geometry.Orientation; import javafx.scene.Scene; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.control.CheckMenuItem; import javafx.scene.control.Menu; import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; import javafx.scene.control.RadioMenuItem; import javafx.scene.control.SeparatorMenuItem; import javafx.scene.control.ToggleGroup; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; import my3d._math3d.*; import my3d._slider_dialog.*; public abstract class _viewer3d extends Application { public abstract void init_scene(_viewport3d vp); public abstract void render(_viewport3d vp); protected int get_view_width() { return 400; } protected int get_view_height() { return 300; } protected _viewport3d create_viewport(Canvas view) { return new _viewport3d(view); } public Stage stage; public Canvas view; public _viewport3d vp; private int min_x = -90, max_x = 90, min_y = 180, max_y = -180; private double round_x = -30, round_y = 30; @Override public void start(Stage stage) throws Exception { this.stage = stage; BorderPane border_pane = new BorderPane(); stage.setScene(new Scene(border_pane)); view = new Canvas(get_view_width(), get_view_height()); border_pane.setCenter(view); vp = create_viewport(view); init_scene(vp); // スライダ _slider slider; if (min_x == max_x) { round_x = min_x; } else { slider = new _slider(min_x, max_x, round_x, Orientation.VERTICAL, (val) -> { round_x = val; set_angle(round_x, round_y); repaint(); }); border_pane.setRight(slider); } if (min_y == max_y) { round_y = min_y; } else { slider = new _slider(min_y, max_y, round_y, Orientation.HORIZONTAL, (val) -> { round_y = val; set_angle(round_x, round_y); repaint(); }); border_pane.setBottom(slider); } // メニュー init_menu(); MenuBar bar = new MenuBar(); if (!app_menu.getItems().isEmpty()) bar.getMenus().add(app_menu); if (!param_menu.getItems().isEmpty()) bar.getMenus().add(param_menu); if (!setting_menu.getItems().isEmpty()) bar.getMenus().add(setting_menu); border_pane.setTop(bar); stage.setTitle("3Dサンプル"); stage.setResizable(false); stage.show(); set_angle(round_x, round_y); repaint(); } public void repaint() { vp.begin_render(); render(vp); vp.end_render(); draw(vp.get_graphics_context()); } private _matrix m_buf_x = new _matrix(), m_buf_y = new _matrix(); private void set_angle(double tx, double ty) { tx *= Math.PI / 180; ty *= Math.PI / 180; _math3d.mul( _matrix.rot_x(tx, m_buf_x), _matrix.rot_y(ty, m_buf_y), vp.scene_transform.orientation); } protected void set_scene_angle(double x, double y) { round_x = x; round_y = y; } public void set_vert_slider_range(int min, int max) { min_x = min; max_x = max; } public void set_horz_slider_range(int min, int max) { min_y = min; max_y = max; } protected void init_menu() {} private Menu app_menu = new Menu("メニュー"); private Menu param_menu = new Menu("パラメータ"); private Menu setting_menu = new Menu("設定"); public static interface _menu_listener { public void menu_selected(int index); } public ArrayList<MenuItem> add_app_menu( _menu_listener lis, String... commands) { return add_menu(app_menu, lis, commands); } public ArrayList<MenuItem> add_param_menu( _menu_listener lis, String... commands) { return add_menu(param_menu, lis, commands); } public ArrayList<MenuItem> add_setting_menu( _menu_listener lis, String... commands) { return add_menu(setting_menu, lis, commands); } private ArrayList<MenuItem> add_menu( Menu menu, _menu_listener lis, String... commands) { ArrayList<MenuItem> items = new ArrayList<>(); MenuItem item; RadioMenuItem radio; ToggleGroup group = null; for (String cmd : commands) { if (cmd.equals("---")) { group = null; menu.getItems().add(new SeparatorMenuItem()); continue; } else if (cmd.startsWith("c")) { item = new CheckMenuItem(cmd.substring(1)); } else if (cmd.startsWith("r")) { if (group == null) group = new ToggleGroup(); radio = new RadioMenuItem(cmd.substring(1)); radio.setToggleGroup(group); item = radio; } else { item = new MenuItem(cmd.startsWith("-") ? cmd.substring(1) : cmd); } item.setOnAction((evt) -> { lis.menu_selected(items.indexOf(evt.getSource())); }); items.add(item); menu.getItems().add(item); } return items; } protected void draw(GraphicsContext gc) {} }