Created
December 4, 2012 00:53
-
-
Save windy1/4199490 to your computer and use it in GitHub Desktop.
ScreenLoader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This file is part of Vanilla. | |
* | |
* Copyright (c) 2011-2012, VanillaDev <http://www.spout.org/> | |
* Vanilla is licensed under the SpoutDev License Version 1. | |
* | |
* Vanilla is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU Lesser General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* In addition, 180 days after any changes are published, you can use the | |
* software, incorporating those changes, under the terms of the MIT license, | |
* as described in the SpoutDev License Version 1. | |
* | |
* Vanilla is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU Lesser General Public License for more details. | |
* | |
* You should have received a copy of the GNU Lesser General Public License, | |
* the MIT license and the SpoutDev License Version 1 along with this program. | |
* If not, see <http://www.gnu.org/licenses/> for the GNU Lesser General Public | |
* License and see <http://www.spout.org/SpoutDevLicenseV1.txt> for the full license, | |
* including the MIT license. | |
*/ | |
package net.windwaker.gck.resource; | |
import java.awt.Color; | |
import java.io.InputStream; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.Method; | |
import java.util.List; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import net.windwaker.gck.GckPlugin; | |
import org.spout.api.Spout; | |
import org.spout.api.component.components.WidgetComponent; | |
import org.spout.api.exception.ConfigurationException; | |
import org.spout.api.gui.Screen; | |
import org.spout.api.gui.Widget; | |
import org.spout.api.math.Rectangle; | |
import org.spout.api.render.RenderMaterial; | |
import org.spout.api.resource.BasicResourceLoader; | |
import org.spout.api.util.config.yaml.YamlConfiguration; | |
public class ScreenLoader extends BasicResourceLoader<ScreenResource> { | |
private final static Logger logger = GckPlugin.getInstance().getLogger(); | |
@Override | |
public String getFallbackResourceName() { | |
return "screen://gck/screen.yml"; | |
} | |
@Override | |
public String getProtocol() { | |
return "screen"; | |
} | |
@Override | |
public String[] getExtensions() { | |
return new String[] {"yml"}; | |
} | |
@Override | |
public ScreenResource getResource(InputStream stream) { | |
final YamlConfiguration data = new YamlConfiguration(stream); | |
try { | |
data.load(); | |
} catch (ConfigurationException e) { | |
logger.log(Level.SEVERE, "Error loading screen resource.", e); | |
} | |
data.setPathSeparator("/"); | |
Screen screen = new Screen(); | |
for (String widgetName : data.getNode("widgets").getKeys(false)) { | |
Widget widget = new Widget(); | |
for (String componentName : data.getNode("widgets/" + widgetName).getKeys(false)) { | |
// Add the specified component | |
Class componentType = null; | |
try { | |
componentType = Class.forName(componentName); | |
} catch (ClassNotFoundException e) { | |
logger.log(Level.SEVERE, "Component class '" + componentName + "' not found.", e); | |
} | |
WidgetComponent component = (WidgetComponent) widget.add(componentType); | |
// Set the specified arguments in the component | |
for (String arg : data.getNode("widgets/" + widgetName + "/" + componentName).getKeys(false)) { | |
if (arg.equalsIgnoreCase("source")) { | |
invoke(component, "setSource", Rectangle.class, getRectangle(data, "widgets/" + widgetName + "/" + componentName + "/source")); | |
} else if (arg.equalsIgnoreCase("sprite")) { | |
invoke(component, "setSprite", Rectangle.class, getRectangle(data, "widgets/" + widgetName + "/" + componentName + "/sprite")); | |
} else if (arg.equalsIgnoreCase("color")) { | |
invoke(component, "setColor", Color.class, getColor(data, "widgets/" + widgetName + "/" + componentName + "/color")); | |
} else if (arg.equalsIgnoreCase("material")) { | |
invoke(component, "setRenderMaterial", RenderMaterial.class, | |
Spout.getEngine().getFilesystem().getResource( | |
data.getNode("widgets/" + widgetName + "/" + componentName + "/material").getString())); | |
} | |
} | |
} | |
screen.attachWidget(GckPlugin.getInstance(), widget); | |
} | |
return new ScreenResource(screen); | |
} | |
private Color getColor(YamlConfiguration data, String arg) { | |
String colorName = data.getNode(arg).getString(); | |
if (colorName == null) { | |
List<Double> doubles = data.getNode(arg).getDoubleList(); | |
if (doubles == null || doubles.size() != 4) { | |
throw new IllegalArgumentException("Invalid arguments supplied for Color."); | |
} | |
Double[] args = doubles.toArray(new Double[doubles.size()]); | |
return new Color(args[0].floatValue(), args[1].floatValue(), args[2].floatValue(), args[3].floatValue()); | |
} else { | |
Color color = null; | |
try { | |
Field field = Color.class.getField(colorName); | |
field.setAccessible(true); | |
Object obj = field.get(null); | |
if (!(obj instanceof Color)) { | |
throw new IllegalArgumentException("Specified color is not a color."); | |
} | |
color = (Color) obj; | |
} catch (NoSuchFieldException e) { | |
logger.log(Level.SEVERE, "Specified color was not found.", e); | |
} catch (Exception e) { | |
logger.log(Level.SEVERE, "Error getting color from name.", e); | |
} | |
return color; | |
} | |
} | |
private Rectangle getRectangle(YamlConfiguration data, String arg) { | |
List<Double> doubles = data.getNode(arg).getDoubleList(); | |
if (doubles == null || doubles.size() != 4) { | |
throw new IllegalArgumentException("Invalid arguments supplied for Rectangle."); | |
} | |
Double[] args = doubles.toArray(new Double[doubles.size()]); | |
return new Rectangle(args[0].floatValue(), args[1].floatValue(), args[2].floatValue(), args[3].floatValue()); | |
} | |
private void invoke(WidgetComponent component, String methodName, Class argType, Object arg) { | |
try { | |
Method method = component.getClass().getMethod(methodName, argType); | |
method.setAccessible(true); | |
method.invoke(component, arg); | |
} catch (Exception e) { | |
logger.log(Level.SEVERE, "Error while invoking component method.", e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment