From fe746cba795af3b6498c38dc8f5a5bd95962524f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20Mart=C3=ADn?= Date: Fri, 31 May 2013 14:07:31 +0200 Subject: [PATCH] Added base entity manager Entity = Object inside a scene --- .../studio/static/coffee/loader.coffee | 10 +-- .../studio/static/coffee/objects.coffee | 46 ++++++++++++ .../studio/static/coffee/studio.coffee | 74 ++++++++++++++++--- minecraftcodex/studio/static/sass/_fixes.sass | 3 + .../studio/templates/studio/main.html | 14 +++- .../studio/templates/studio/modals.html | 2 +- .../studio/templates/studio/templates.html | 19 +++++ 7 files changed, 146 insertions(+), 22 deletions(-) create mode 100644 minecraftcodex/studio/static/coffee/objects.coffee create mode 100644 minecraftcodex/studio/templates/studio/templates.html diff --git a/minecraftcodex/studio/static/coffee/loader.coffee b/minecraftcodex/studio/static/coffee/loader.coffee index 3349e31..23f015e 100644 --- a/minecraftcodex/studio/static/coffee/loader.coffee +++ b/minecraftcodex/studio/static/coffee/loader.coffee @@ -1,7 +1,7 @@ window.onload = -> # Studio if window.Studio - window.Studio.init '.studio-canvas', 320, 240 + window.Studio.init '.studio-canvas', 640, 480 window.modalManager.studio = window.Studio # Textures @@ -43,7 +43,7 @@ window.onload = -> target = $(this).attr 'data-target' $(target).toggle 'fast' - # TESTS - $('[data-studio="change-texture"]').click -> - texture = $(this).attr('data-texture') - window.Studio.changeTexture texture + # Objects + $('.btn-addobject').click -> + obj = $('.object-list').val() + window.Studio.objectManager.add obj diff --git a/minecraftcodex/studio/static/coffee/objects.coffee b/minecraftcodex/studio/static/coffee/objects.coffee new file mode 100644 index 0000000..8993126 --- /dev/null +++ b/minecraftcodex/studio/static/coffee/objects.coffee @@ -0,0 +1,46 @@ +class Entity + _object: null + + _id: null + + _visible: true + + _position: + x: 0 + y: 0 + z: 0 + + _rotation: + x: 0 + y: 0 + z: 0 + + _scale: + x: 0 + y: 0 + z: 0 + +class Cube extends Entity + name: 'Cube' + + init: (size) -> + @_shape = new THREE.CubeGeometry size.x, size.y, size.z + @_texture = new THREE.MeshNormalMaterial + + @_object = new THREE.Mesh @_shape, @_texture + @ + +class Sphere extends Entity + name: 'Sphere' + + init: (size) -> + @_shape = new THREE.SphereGeometry size.x, size.y, size.z + @_texture = new THREE.MeshNormalMaterial + + @_object = new THREE.Mesh @_shape, @_texture + @ + + +window.StudioObjects = + cube: Cube + sphere: Sphere diff --git a/minecraftcodex/studio/static/coffee/studio.coffee b/minecraftcodex/studio/static/coffee/studio.coffee index 4dd5c39..6bfbd62 100644 --- a/minecraftcodex/studio/static/coffee/studio.coffee +++ b/minecraftcodex/studio/static/coffee/studio.coffee @@ -19,6 +19,56 @@ Studio = objects: [] _objects: {} + # OBJECT MANAGER + objectManager: + add: (object) -> + if object of window.StudioObjects + obj = new window.StudioObjects[object] + obj.init + x: 16 + y: 16 + z: 16 + obj._id = @studio.objects.length + 1 + @studio.objects.push + type: object + id: obj._id + + @studio._objects[obj._id] = obj + @studio.scene.add @studio._objects[obj._id]._object + + context = + object_id: obj._id + name: obj.name + + template = Handlebars.compile $('#entity-template-simple').html() + html = template(context) + $('.entities-list').append(html) + @setHandlers $(".entities-list .entity-#{obj._id}") + + + list: -> + return @studio._objects + + setHandlers: (dom) -> + _this = @ + dom.find('.btn-edit').click -> + console.log 'edit' + + dom.find('.btn-remove').click -> + _this.remove $(@).parents('[data-objectid]').attr('data-objectid') + + dom.find('.check-visible').change -> + console.log 'toggle!' + + remove: (object_id) -> + if object_id of @studio._objects + obj = @studio._objects[object_id] + @studio.scene.remove obj._object + delete @studio._objects[object_id] + $(".entities-list .entity-#{object_id}").remove() + + + # Methods checkWebGLsupport: -> return !!window.WebGLRenderingContext; @@ -52,18 +102,6 @@ Studio = @onCameraChange @_cameraType - # Tests - changeTexture: (path) -> - @scene.remove @object - - texture = new THREE.ImageUtils.loadTexture path - texture.minFilter = THREE.NearestFilter - texture.magFilter = THREE.NearestFilter - material = new THREE.MeshLambertMaterial map: texture - @object = new THREE.Mesh new THREE.CubeGeometry(16, 16, 16), material - @object.rotation.set Math.PI/6, (Math.PI/4)*-1, 0 - @scene.add @object - reset: -> cancelAnimationFrame @_animationFrame @renderer.domElement.remove() @@ -73,6 +111,7 @@ Studio = if not @checkWebGLsupport() return false + # Renderer @domElement = document.querySelector dom @_dom = dom @@ -81,14 +120,25 @@ Studio = @domElement.appendChild @renderer.domElement + # Scene @scene = new THREE.Scene() + # Populate + + # Add all objects to object select + objectsDom = $('select.object-list') + for i of window.StudioObjects + obj = window.StudioObjects[i] + objectsDom.append "" + # test @light = new THREE.DirectionalLight 0xffffff @light.position.set(1, 20, 60).normalize() @light.intensity = 1.6 @scene.add @light + @objectManager.studio = @ + #@object = new THREE.Mesh new THREE.CubeGeometry(16, 16, 16), new THREE.MeshNormalMaterial() #@scene.add @object diff --git a/minecraftcodex/studio/static/sass/_fixes.sass b/minecraftcodex/studio/static/sass/_fixes.sass index ee63b3c..d68a86d 100644 --- a/minecraftcodex/studio/static/sass/_fixes.sass +++ b/minecraftcodex/studio/static/sass/_fixes.sass @@ -11,3 +11,6 @@ .valing-top vertical-align: top + +#objects table + margin-bottom: 0 diff --git a/minecraftcodex/studio/templates/studio/main.html b/minecraftcodex/studio/templates/studio/main.html index eb233d5..3e04a0b 100644 --- a/minecraftcodex/studio/templates/studio/main.html +++ b/minecraftcodex/studio/templates/studio/main.html @@ -21,6 +21,7 @@ + @@ -83,7 +84,7 @@

Textures

- {% for item in textures %} + {#{% for item in textures %}
@@ -91,7 +92,7 @@
- {% endfor %} + {% endfor %}#}
Mouseover a texture
@@ -137,11 +138,15 @@

Objects


- - + + + +
@@ -158,6 +163,7 @@
{% include "studio/modals.html" %} +{% include "studio/templates.html" %} {% endblock %} {% block footer %}{% endblock %} diff --git a/minecraftcodex/studio/templates/studio/modals.html b/minecraftcodex/studio/templates/studio/modals.html index 3b91b70..b93f3e3 100644 --- a/minecraftcodex/studio/templates/studio/modals.html +++ b/minecraftcodex/studio/templates/studio/modals.html @@ -1,4 +1,4 @@ - +