diff --git a/src/main/java/dan200/computercraft/client/gui/GuiComputer.java b/src/main/java/dan200/computercraft/client/gui/GuiComputer.java index 7a6ad6e3a..d73678858 100644 --- a/src/main/java/dan200/computercraft/client/gui/GuiComputer.java +++ b/src/main/java/dan200/computercraft/client/gui/GuiComputer.java @@ -95,7 +95,7 @@ protected void keyTyped(char c, int k) throws IOException } else { - m_terminal.keyTyped( c, k ); + if( m_terminal.keyTyped( c, k ) ) keyHandled = true; } } @@ -123,7 +123,7 @@ public void handleMouseInput() throws IOException public void handleKeyboardInput() throws IOException { super.handleKeyboardInput(); - m_terminal.handleKeyboardInput(); + if( m_terminal.handleKeyboardInput() ) keyHandled = true; } @Override diff --git a/src/main/java/dan200/computercraft/client/gui/GuiTurtle.java b/src/main/java/dan200/computercraft/client/gui/GuiTurtle.java index 5a2197ef0..6100b9035 100644 --- a/src/main/java/dan200/computercraft/client/gui/GuiTurtle.java +++ b/src/main/java/dan200/computercraft/client/gui/GuiTurtle.java @@ -95,7 +95,7 @@ protected void keyTyped(char c, int k) throws IOException } else { - m_terminalGui.keyTyped( c, k ); + if( m_terminalGui.keyTyped( c, k ) ) keyHandled = true; } } @@ -119,7 +119,7 @@ public void handleMouseInput() throws IOException public void handleKeyboardInput() throws IOException { super.handleKeyboardInput(); - m_terminalGui.handleKeyboardInput(); + if( m_terminalGui.handleKeyboardInput() ) keyHandled = true; } protected void drawSelectionSlot( boolean advanced ) diff --git a/src/main/java/dan200/computercraft/client/gui/widgets/Widget.java b/src/main/java/dan200/computercraft/client/gui/widgets/Widget.java index ba99a9fda..69a98a0bd 100644 --- a/src/main/java/dan200/computercraft/client/gui/widgets/Widget.java +++ b/src/main/java/dan200/computercraft/client/gui/widgets/Widget.java @@ -20,8 +20,6 @@ public abstract class Widget extends Gui { - private WidgetContainer m_parent; - private boolean m_visible; private int m_xPosition; private int m_yPosition; private int m_width; @@ -29,64 +27,12 @@ public abstract class Widget extends Gui protected Widget( int x, int y, int width, int height ) { - m_parent = null; - m_visible = true; m_xPosition = x; m_yPosition = y; m_width = width; m_height = height; } - public WidgetContainer getRoot() - { - if( m_parent != null ) - { - return m_parent.getRoot(); - } - else if( this instanceof WidgetContainer ) - { - return (WidgetContainer)this; - } - return null; - } - - public WidgetContainer getParent() - { - return m_parent; - } - - public void setParent( WidgetContainer parent ) - { - m_parent = parent; - } - - public boolean isObscured() - { - if( m_parent != null ) - { - Widget parentModalWidget = m_parent.getModalWidget(); - if( parentModalWidget == null ) - { - return m_parent.isObscured(); - } - else - { - return (parentModalWidget != this); - } - } - return false; - } - - public boolean isVisible() - { - return m_visible && (m_parent == null || m_parent.isVisible()); - } - - public void setVisible( boolean visible ) - { - m_visible = visible; - } - public int getXPosition() { return m_xPosition; @@ -97,16 +43,6 @@ public int getYPosition() return m_yPosition; } - public int getAbsoluteXPosition() - { - return m_xPosition + (m_parent != null ? m_parent.getAbsoluteXPosition() : 0); - } - - public int getAbsoluteYPosition() - { - return m_yPosition + (m_parent != null ? m_parent.getAbsoluteYPosition() : 0); - } - public int getWidth() { return m_width; @@ -117,18 +53,6 @@ public int getHeight() return m_height; } - public void setPosition( int x, int y ) - { - m_xPosition = x; - m_yPosition = y; - } - - public void resize( int width, int height ) - { - m_width = width; - m_height = height; - } - public void update() { } @@ -137,246 +61,21 @@ public void draw( Minecraft mc, int xOrigin, int yOrigin, int mouseX, int mouseY { } - public void drawForeground( Minecraft mc, int xOrigin, int yOrigin, int mouseX, int mouseY ) - { - } - - public void modifyMousePosition( MousePos pos ) - { - } - public void handleMouseInput( int mouseX, int mouseY ) { } - public void handleKeyboardInput() + public boolean handleKeyboardInput() { + return false; } public void mouseClicked( int mouseX, int mouseY, int mouseButton ) { } - public void keyTyped( char c, int k ) - { - } - - public boolean suppressItemTooltips( Minecraft mc, int xOrigin, int yOrigin, int mouseX, int mouseY ) + public boolean keyTyped( char c, int k ) { return false; } - - public boolean suppressKeyPress( char c, int k ) - { - return false; - } - - protected void drawFullImage( int x, int y, int w, int h ) - { - Tessellator tessellator = Tessellator.getInstance(); - tessellator.getBuffer().begin( GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX ); - tessellator.getBuffer().pos( x + 0, y + h, this.zLevel ).tex( 0.0, 1.0 ).endVertex(); - tessellator.getBuffer().pos( x + w, y + h, this.zLevel ).tex( 1.0, 1.0 ).endVertex(); - tessellator.getBuffer().pos( x + w, y + 0, this.zLevel ).tex( 1.0, 0.0 ).endVertex(); - tessellator.getBuffer().pos( x + 0, y + 0, this.zLevel ).tex( 0.0, 0.0 ).endVertex(); - tessellator.draw(); - } - - protected void drawT3( int x, int y, int w, int h, int u, int v, int uw, int vh ) - { - int partW = uw / 3; - - // Draw first bit - super.drawTexturedModalRect( x, y, u, v, partW, vh ); - - // Draw middle bits - int middleBits = Math.max( (w - 2 * partW) / partW, 0 ); - for( int j=0; j mc.currentScreen.width ) - { - startX -= width + 24; - if( startX < 24 ) - { - startX = 24; - } - } - - float oldZLevel = this.zLevel; - try - { - this.zLevel = 300.0F; - - int height = 10 * lines.length - 2; - int j1 = -267386864; - this.drawGradientRect( startX - 3, startY - 4, startX + width + 3, startY - 3, j1, j1 ); - this.drawGradientRect( startX - 3, startY + height + 3, startX + width + 3, startY + height + 4, j1, j1 ); - this.drawGradientRect( startX - 3, startY - 3, startX + width + 3, startY + height + 3, j1, j1 ); - this.drawGradientRect( startX - 4, startY - 3, startX - 3, startY + height + 3, j1, j1 ); - - this.drawGradientRect( startX + width + 3, startY - 3, startX + width + 4, startY + height + 3, j1, j1 ); - int k1 = 1347420415; - int l1 = ( k1 & 16711422 ) >> 1 | k1 & -16777216; - this.drawGradientRect( startX - 3, startY - 3 + 1, startX - 3 + 1, startY + height + 3 - 1, k1, l1 ); - this.drawGradientRect( startX + width + 2, startY - 3 + 1, startX + width + 3, startY + height + 3 - 1, k1, l1 ); - this.drawGradientRect( startX - 3, startY - 3, startX + width + 3, startY - 3 + 1, k1, k1 ); - this.drawGradientRect( startX - 3, startY + height + 2, startX + width + 3, startY + height + 3, l1, l1 ); - - GlStateManager.disableDepth(); - try - { - for( int i = 0; i < lines.length; ++i ) - { - String line = lines[ i ]; - fontRenderer.drawStringWithShadow( line, startX, startY + i * 10, 0xffffffff ); - } - } - finally - { - GlStateManager.color( 1.0f, 1.0f, 1.0f, 1.0f ); - GlStateManager.enableDepth(); - } - } - finally - { - this.zLevel = oldZLevel; - } - } - - protected void drawItemStack( int x, int y, @Nonnull ItemStack stack ) - { - if( !stack.isEmpty() ) - { - GlStateManager.color( 1.0f, 1.0f, 1.0f, 1.0f ); - GlStateManager.enableLighting(); - GlStateManager.enableRescaleNormal(); - RenderHelper.enableGUIStandardItemLighting(); - OpenGlHelper.setLightmapTextureCoords( OpenGlHelper.lightmapTexUnit, 240.0f, 240.0f ); - try - { - Minecraft mc = Minecraft.getMinecraft(); - RenderItem renderItem = mc.getRenderItem(); - if( renderItem != null ) - { - renderItem.renderItemAndEffectIntoGUI( stack, x, y ); - renderItem.renderItemOverlayIntoGUI( mc.fontRenderer, stack, x, y, null ); - } - } - finally - { - RenderHelper.disableStandardItemLighting(); - GlStateManager.disableRescaleNormal(); - GlStateManager.disableLighting(); - GlStateManager.enableBlend(); - GlStateManager.blendFunc( GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA ); - GlStateManager.color( 1.0f, 1.0f, 1.0f, 1.0f ); - } - } - } - - protected void drawString( String s, int x, int y, int color ) - { - Minecraft mc = Minecraft.getMinecraft(); - try - { - mc.fontRenderer.drawString( s, x, y, color ); - } - finally - { - GlStateManager.color( 1.0f, 1.0f, 1.0f, 1.0f ); - } - } - - protected int getStringWidth( String s ) - { - Minecraft mc = Minecraft.getMinecraft(); - return mc.fontRenderer.getStringWidth( s ); - } - - protected void playClickSound() - { - Minecraft mc = Minecraft.getMinecraft(); - mc.getSoundHandler().playSound( PositionedSoundRecord.getMasterRecord( SoundEvents.UI_BUTTON_CLICK, 1.0F ) ); - } } diff --git a/src/main/java/dan200/computercraft/client/gui/widgets/WidgetContainer.java b/src/main/java/dan200/computercraft/client/gui/widgets/WidgetContainer.java deleted file mode 100644 index 93418c87e..000000000 --- a/src/main/java/dan200/computercraft/client/gui/widgets/WidgetContainer.java +++ /dev/null @@ -1,336 +0,0 @@ -/* - * This file is part of ComputerCraft - http://www.computercraft.info - * Copyright Daniel Ratcliffe, 2011-2017. Do not distribute without permission. - * Send enquiries to dratcliffe@gmail.com - */ - - -package dan200.computercraft.client.gui.widgets; - -import dan200.computercraft.client.gui.widgets.Widget; -import net.minecraft.client.Minecraft; -import net.minecraft.client.renderer.GlStateManager; - -import java.util.ArrayList; - -public class WidgetContainer extends Widget -{ - private ArrayList m_widgets; - private Widget m_modalWidget; - - public WidgetContainer( int x, int y, int w, int h ) - { - super( x, y, w, h ); - m_widgets = new ArrayList<>(); - m_modalWidget = null; - } - - public void addWidget( Widget widget ) - { - m_widgets.add( widget ); - widget.setParent( this ); - } - - public void setModalWidget( Widget widget ) - { - m_modalWidget = widget; - if( widget != null ) - { - widget.setParent( this ); - } - } - - public Widget getModalWidget() - { - return m_modalWidget; - } - - @Override - public void update() - { - for( Widget m_widget : m_widgets ) - { - m_widget.update(); - } - if( m_modalWidget != null ) - { - m_modalWidget.update(); - } - } - - @Override - public void draw( Minecraft mc, int xOrigin, int yOrigin, int mouseX, int mouseY ) - { - for( Widget widget : m_widgets ) - { - if( widget.isVisible() ) - { - widget.draw( - mc, - xOrigin + getXPosition(), - yOrigin + getYPosition(), - (m_modalWidget == null) ? (mouseX - getXPosition()) : -99, - (m_modalWidget == null) ? (mouseY - getYPosition()) : -99 - ); - } - } - if( m_modalWidget != null ) - { - if( m_modalWidget.isVisible() ) - { - GlStateManager.pushMatrix(); - try - { - GlStateManager.translate( 0.0f, 0.0f, 200.0f ); - m_modalWidget.draw( - mc, - xOrigin + getXPosition(), - yOrigin + getYPosition(), - mouseX - getXPosition(), - mouseY - getYPosition() - ); - } - finally - { - GlStateManager.popMatrix(); - } - } - } - } - - @Override - public void drawForeground( Minecraft mc, int xOrigin, int yOrigin, int mouseX, int mouseY ) - { - for( Widget widget : m_widgets ) - { - if( widget.isVisible() ) - { - widget.drawForeground( - mc, - xOrigin + getXPosition(), - yOrigin + getYPosition(), - (m_modalWidget == null) ? (mouseX - getXPosition()) : -99, - (m_modalWidget == null) ? (mouseY - getYPosition()) : -99 - ); - } - } - - if( m_modalWidget != null ) - { - if( m_modalWidget.isVisible() ) - { - GlStateManager.pushMatrix(); - try - { - GlStateManager.translate( 0.0f, 0.0f, 200.0f ); - m_modalWidget.drawForeground( - mc, - xOrigin + getXPosition(), - yOrigin + getYPosition(), - mouseX - getXPosition(), - mouseY - getYPosition() - ); - } - finally - { - GlStateManager.popMatrix(); - } - } - } - } - - @Override - public void modifyMousePosition( MousePos pos ) - { - pos.x -= getXPosition(); - pos.y -= getYPosition(); - if( m_modalWidget == null ) - { - for( Widget widget : m_widgets ) - { - if( widget.isVisible() ) - { - widget.modifyMousePosition( pos ); - } - } - } - else - { - if( m_modalWidget.isVisible() ) - { - m_modalWidget.modifyMousePosition( pos ); - } - } - pos.x += getXPosition(); - pos.y += getYPosition(); - } - - @Override - public boolean suppressItemTooltips( Minecraft mc, int xOrigin, int yOrigin, int mouseX, int mouseY ) - { - if( m_modalWidget == null ) - { - for( Widget widget : m_widgets ) - { - if( widget.isVisible() ) - { - if( widget.suppressItemTooltips( - mc, - xOrigin + getXPosition(), - yOrigin + getYPosition(), - mouseX - getXPosition(), - mouseY - getYPosition() - ) ) - { - return true; - } - } - } - } - else - { - if( m_modalWidget.isVisible() && m_modalWidget.suppressItemTooltips( - mc, - xOrigin + getXPosition(), - yOrigin + getYPosition(), - mouseX - getXPosition(), - mouseY - getYPosition() - ) ) - { - return true; - } - } - return false; - } - - @Override - public boolean suppressKeyPress( char c, int k ) - { - if( m_modalWidget == null ) - { - for( Widget widget : m_widgets ) - { - if( widget.isVisible() ) - { - if( widget.suppressKeyPress( c, k ) ) - { - return true; - } - } - } - } - else - { - if( m_modalWidget.isVisible() ) - { - if( m_modalWidget.suppressKeyPress( c, k ) ) - { - return true; - } - } - } - return false; - } - - @Override - public void handleMouseInput( int mouseX, int mouseY ) - { - if( m_modalWidget == null ) - { - for( Widget widget : m_widgets ) - { - if( widget.isVisible() ) - { - widget.handleMouseInput( - mouseX - getXPosition(), - mouseY - getYPosition() - ); - } - } - } - else - { - if( m_modalWidget.isVisible() ) - { - m_modalWidget.handleMouseInput( - mouseX - getXPosition(), - mouseY - getYPosition() - ); - } - } - } - - @Override - public void handleKeyboardInput() - { - if( m_modalWidget == null ) - { - for( Widget widget : m_widgets ) - { - if( widget.isVisible() ) - { - widget.handleKeyboardInput(); - } - } - } - else - { - if( m_modalWidget.isVisible() ) - { - m_modalWidget.handleKeyboardInput(); - } - } - } - - @Override - public void mouseClicked( int mouseX, int mouseY, int mouseButton ) - { - if( m_modalWidget == null ) - { - for( Widget widget : m_widgets ) - { - if( widget.isVisible() ) - { - widget.mouseClicked( - mouseX - getXPosition(), - mouseY - getYPosition(), - mouseButton - ); - } - } - } - else - { - if( m_modalWidget.isVisible() ) - { - m_modalWidget.mouseClicked( - mouseX - getXPosition(), - mouseY - getYPosition(), - mouseButton - ); - } - } - } - - @Override - public void keyTyped( char c, int k ) - { - if( m_modalWidget == null ) - { - for( Widget widget : m_widgets ) - { - if( widget.isVisible() ) - { - widget.keyTyped( c, k ); - } - } - } - else - { - if( m_modalWidget.isVisible() ) - { - m_modalWidget.keyTyped( c, k ); - } - } - } -} diff --git a/src/main/java/dan200/computercraft/client/gui/widgets/WidgetTerminal.java b/src/main/java/dan200/computercraft/client/gui/widgets/WidgetTerminal.java index 0544558a7..a4aae498f 100644 --- a/src/main/java/dan200/computercraft/client/gui/widgets/WidgetTerminal.java +++ b/src/main/java/dan200/computercraft/client/gui/widgets/WidgetTerminal.java @@ -27,7 +27,7 @@ public class WidgetTerminal extends Widget { private static final ResourceLocation background = new ResourceLocation( "computercraft", "textures/gui/term_background.png" ); - private static float TERMINATE_TIME = 0.5f; + private static final float TERMINATE_TIME = 0.5f; private final IComputerContainer m_computer; @@ -41,7 +41,6 @@ public class WidgetTerminal extends Widget private boolean m_focus; private boolean m_allowFocusLoss; - private boolean m_locked; private int m_leftMargin; private int m_rightMargin; @@ -69,7 +68,6 @@ public WidgetTerminal( int x, int y, int termWidth, int termHeight, IComputerCon m_focus = false; m_allowFocusLoss = true; - m_locked = false; m_leftMargin = leftMargin; m_rightMargin = rightMargin; @@ -85,15 +83,10 @@ public void setAllowFocusLoss( boolean allowFocusLoss ) m_focus = m_focus || !allowFocusLoss; } - public void setLocked( boolean locked ) - { - m_locked = locked; - } - @Override - public void keyTyped( char ch, int key ) + public boolean keyTyped( char ch, int key ) { - if( m_focus && !m_locked ) + if( m_focus ) { // Ctrl+V for paste if( ch == 22 ) @@ -134,13 +127,14 @@ else if( newLineIndex2 >= 0 ) } ); } } - return; + return true; } // Regular keys normally if( m_terminateTimer <= 0.0f && m_rebootTimer <= 0.0f && m_shutdownTimer <= 0.0f ) { boolean repeat = Keyboard.isRepeatEvent(); + boolean handled = false; if( key > 0 ) { if( !repeat ) @@ -152,6 +146,7 @@ else if( newLineIndex2 >= 0 ) queueEvent( "key", new Object[]{ key, repeat } ); + handled = true; } if( (ch >= 32 && ch <= 126) || (ch >= 160 && ch <= 255) ) // printable chars in byte range @@ -160,9 +155,14 @@ else if( newLineIndex2 >= 0 ) queueEvent( "char", new Object[]{ Character.toString( ch ) } ); + handled = true; } + + return handled; } } + + return false; } @Override @@ -179,7 +179,7 @@ public void mouseClicked( int mouseX, int mouseY, int button ) if( m_focus ) { IComputer computer = m_computer.getComputer(); - if( !m_locked && computer != null && computer.isColour() && button >= 0 && button <= 2 ) + if( computer != null && computer.isColour() && button >= 0 && button <= 2 ) { Terminal term = computer.getTerminal(); if( term != null ) @@ -210,23 +210,27 @@ public void mouseClicked( int mouseX, int mouseY, int button ) } @Override - public void handleKeyboardInput() + public boolean handleKeyboardInput() { - for( int i=m_keysDown.size()-1; i>=0; --i ) + boolean handled = false; + for( int i = m_keysDown.size() - 1; i >= 0; --i ) { int key = m_keysDown.get( i ); if( !Keyboard.isKeyDown( key ) ) { m_keysDown.remove( i ); - if( m_focus && !m_locked ) + if( m_focus ) { // Queue the "key_up" event queueEvent( "key_up", new Object[]{ key } ); + handled = true; } } } + + return handled; } @Override @@ -247,7 +251,7 @@ public void handleMouseInput( int mouseX, int mouseY ) if( m_lastClickButton >= 0 && !Mouse.isButtonDown( m_lastClickButton ) ) { - if( m_focus && !m_locked ) + if( m_focus ) { computer.queueEvent( "mouse_up", new Object[]{ m_lastClickButton + 1, charX + 1, charY + 1 @@ -262,7 +266,7 @@ public void handleMouseInput( int mouseX, int mouseY ) return; } - if( m_focus && !m_locked ) + if( m_focus ) { if( wheelChange < 0 ) { @@ -294,7 +298,7 @@ else if( wheelChange > 0 ) public void update() { // Handle special keys - if( m_focus && !m_locked && (Keyboard.isKeyDown( 29 ) || Keyboard.isKeyDown( 157 )) ) + if( m_focus && (Keyboard.isKeyDown( 29 ) || Keyboard.isKeyDown( 157 )) ) { // Ctrl+T for terminate if( Keyboard.isKeyDown( 20 ) ) @@ -446,19 +450,6 @@ public void draw( Minecraft mc, int xOrigin, int yOrigin, int mouseX, int mouseY } } - @Override - public boolean suppressKeyPress( char c, int k ) - { - if( m_focus ) - { - return k != 1; // escape - } - else - { - return false; - } - } - private void queueEvent( String event ) { IComputer computer = m_computer.getComputer();