|
| 1 | +/* |
| 2 | + * $Id$ |
| 3 | + * |
| 4 | + * Copyright (c) 2016, Simsilica, LLC |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions |
| 9 | + * are met: |
| 10 | + * |
| 11 | + * 1. Redistributions of source code must retain the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer. |
| 13 | + * |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | + * notice, this list of conditions and the following disclaimer in |
| 16 | + * the documentation and/or other materials provided with the |
| 17 | + * distribution. |
| 18 | + * |
| 19 | + * 3. Neither the name of the copyright holder nor the names of its |
| 20 | + * contributors may be used to endorse or promote products derived |
| 21 | + * from this software without specific prior written permission. |
| 22 | + * |
| 23 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 24 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 25 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 26 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 27 | + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 28 | + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 29 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 30 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 31 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 32 | + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 33 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 34 | + * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 | + */ |
| 36 | + |
| 37 | +package example; |
| 38 | + |
| 39 | +import com.jme3.app.Application; |
| 40 | +import com.jme3.app.state.BaseAppState; |
| 41 | +import com.jme3.math.*; |
| 42 | +import com.jme3.scene.*; |
| 43 | + |
| 44 | +import com.simsilica.lemur.*; |
| 45 | +import com.simsilica.lemur.component.SpringGridLayout; |
| 46 | +import com.simsilica.lemur.style.ElementId; |
| 47 | + |
| 48 | +/** |
| 49 | + * A basic "login" state that provides a simple UI for logging in |
| 50 | + * once a connection has been established. This just manages the UI |
| 51 | + * and calls back to the ConnectState to do the actual 'work'. |
| 52 | + * |
| 53 | + * @author Paul Speed |
| 54 | + */ |
| 55 | +public class LoginState extends BaseAppState { |
| 56 | + |
| 57 | + private Container loginPanel; |
| 58 | + private TextField nameField; |
| 59 | + |
| 60 | + public LoginState() { |
| 61 | + } |
| 62 | + |
| 63 | + protected void join() { |
| 64 | + |
| 65 | + String name = nameField.getText().trim(); |
| 66 | + if( getState(ConnectState.class).join(nameField.getText()) ) { |
| 67 | + getStateManager().detach(this); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + protected void cancel() { |
| 72 | + getState(ConnectState.class).disconnect(); |
| 73 | + getStateManager().detach(this); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + protected void initialize( Application app ) { |
| 78 | + loginPanel = new Container(); |
| 79 | + loginPanel.addChild(new Label("Login", new ElementId("title"))); |
| 80 | + |
| 81 | + Container props = loginPanel.addChild(new Container(new SpringGridLayout(Axis.Y, Axis.X, FillMode.None, FillMode.Last))); |
| 82 | + props.setBackground(null); |
| 83 | + props.addChild(new Label("Name:")); |
| 84 | + nameField = props.addChild(new TextField(System.getProperty("user.name")), 1); |
| 85 | + |
| 86 | + Container buttons = loginPanel.addChild(new Container(new SpringGridLayout(Axis.X, Axis.Y))); |
| 87 | + buttons.setBackground(null); |
| 88 | + buttons.setLayout(new SpringGridLayout(Axis.X, Axis.Y)); |
| 89 | + buttons.addChild(new ActionButton(new CallMethodAction("Join", this, "join"))); |
| 90 | + buttons.addChild(new ActionButton(new CallMethodAction("Cancel", this, "cancel"))); |
| 91 | + |
| 92 | + float scale = 1.5f * getState(MainMenuState.class).getStandardScale(); |
| 93 | + loginPanel.setLocalScale(scale); |
| 94 | + |
| 95 | + Vector3f prefs = loginPanel.getPreferredSize().clone(); |
| 96 | + prefs.x = Math.max(300, prefs.x); |
| 97 | + loginPanel.setPreferredSize(prefs.clone()); |
| 98 | + |
| 99 | + // Now account for scaling |
| 100 | + prefs.multLocal(scale); |
| 101 | + |
| 102 | + int width = app.getCamera().getWidth(); |
| 103 | + int height = app.getCamera().getHeight(); |
| 104 | + |
| 105 | + loginPanel.setLocalTranslation(width * 0.5f - prefs.x * 0.5f, height * 0.5f + prefs.y * 0.5f, 10); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + protected void cleanup( Application app ) { |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + protected void onEnable() { |
| 114 | + Node root = ((Main)getApplication()).getGuiNode(); |
| 115 | + root.attachChild(loginPanel); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + protected void onDisable() { |
| 120 | + loginPanel.removeFromParent(); |
| 121 | + } |
| 122 | +} |
0 commit comments