Exemplo n.º 1
0
def GetCompositorChainInfo():
    global _debugRttAtlases
    DisposeCompositorChainInfo()
    vp = Client.Instance.Viewport
    chain = CompositorManager.Instance.GetCompositorChain(vp)
    rv = []
    for inst in chain.Instances:
        if inst.Enabled:
            entry = [inst.Name, []]
            for texDef in inst.Technique.TextureDefinitions:
                instName = inst.GetTextureInstanceName(texDef.Name)
                tex = TextureManager.Instance.GetByName(instName)
                atlas = AtlasManager.Instance.CreateAtlas(tex.Name, tex)
                _debugRttAtlases.append(atlas)
                atlas.DefineImage("RttImage", PointF(0, 0),
                                  SizeF(tex.Width, tex.Height))
                entry[1].append([texDef.Name, tex.Name])
            rv.append(entry)
    return rv
Exemplo n.º 2
0
    def OnDrawItem(self, sender, e):
        if e.Index < 0: return

        g = e.Graphics

        ## Custom draw... an icon and multi-line text

        # Use our own size to preserve the border line
        myBounds = e.Bounds
        myBounds.Height -= self.SPACING

        imageRect = myBounds.MemberwiseClone()
        imageRect.Inflate(-1, -1)
        imageRect.Width = self.icon.Width
        imageRect.X += (self.ICON_SPACE - self.icon.Width) / 2
        imageRect.Y += (imageRect.Height - self.icon.Height) / 2
        imageRect.Height = self.icon.Height

        textRect = myBounds.MemberwiseClone()
        textRect.X += self.ICON_SPACE
        textRect.Width -= self.ICON_SPACE
        textRect.Height -= 1
        textRect.Inflate(-1, -1)

        if ((e.State & DrawItemState.Selected) == DrawItemState.Selected):
            self.textBrush.Color = SystemColors.Highlight
            g.FillRectangle(self.textBrush, myBounds)
            self.textBrush.Color = SystemColors.HighlightText
        else:
            self.textBrush.Color = SystemColors.Window
            g.FillRectangle(self.textBrush, myBounds)
            self.textBrush.Color = SystemColors.WindowText

        g.DrawImage(self.icon, imageRect)
        g.DrawString(sender.Items[e.Index], e.Font, self.textBrush,
                     PointF(textRect.X, textRect.Y))

        # Border line (note that Bottom is actually outside our bounds)
        g.DrawLine(Pens.LightGray, Point(e.Bounds.Left, e.Bounds.Bottom - 1),
                   Point(e.Bounds.Right, e.Bounds.Bottom - 1))
        e.DrawFocusRectangle()
Exemplo n.º 3
0
    def test_sys_drawing(self):
        from IronPythonTest import DaysInt, DaysShort, DaysLong, DaysSByte, DaysByte, DaysUShort, DaysUInt, DaysULong
        from System.Drawing import Point, Size, PointF, SizeF, Rectangle, RectangleF
        x = Point()
        self.assertTrue(x == Point(0, 0))
        x = Size()
        self.assertTrue(x == Size(0, 0))
        x = PointF()
        self.assertTrue(x == PointF(0, 0))
        x = SizeF()
        self.assertTrue(x == SizeF(0, 0))
        x = Rectangle()
        self.assertTrue(x == Rectangle(0, 0, 0, 0))
        x = RectangleF()
        self.assertTrue(x == RectangleF(0, 0, 0, 0))

        p = Point(3, 4)
        s = Size(2, 9)

        q = p + s
        self.assertTrue(q == Point(5, 13))
        self.assertTrue(q != Point(13, 5))
        q = p - s
        self.assertTrue(q == Point(1, -5))
        self.assertTrue(q != Point(0, 4))
        q += s
        self.assertTrue(q == Point(3, 4))
        self.assertTrue(q != Point(2, 4))
        q -= Size(1, 2)
        self.assertTrue(q == Point(2, 2))
        self.assertTrue(q != Point(1))

        t = s
        self.assertTrue(t == s)
        self.assertTrue(t != s - Size(1, 0))
        t += Size(3, 1)
        self.assertTrue(t == Size(5, 10))
        self.assertTrue(t != Size(5, 0))
        t -= Size(2, 8)
        self.assertTrue(t == Size(3, 2))
        self.assertTrue(t != Size(0, 2))
        t = s + Size(-1, -2)
        self.assertTrue(t == Size(1, 7))
        self.assertTrue(t != Size(1, 5))
        t = s - Size(1, 2)
        self.assertTrue(t == Size(1, 7))
        self.assertTrue(t != Size(1, 3))

        def weekdays(enum):
            return enum.Mon | enum.Tue | enum.Wed | enum.Thu | enum.Fri

        def weekend(enum):
            return enum.Sat | enum.Sun

        def enum_helper(enum):
            days = [
                enum.Mon, enum.Tue, enum.Wed, enum.Thu, enum.Fri, enum.Sat,
                enum.Sun
            ]
            x = enum.Mon | enum.Tue | enum.Wed | enum.Thu | enum.Fri | enum.Sat | enum.Sun
            y = enum.Mon
            for day in days:
                y |= day
            self.assertTrue(x == y)
            self.assertFalse(x != y)
            if x == y:  # EqualRetBool
                b = True
            else:
                b = False
            self.assertTrue(b)

            self.assertTrue(x == weekdays(enum) | weekend(enum))
            self.assertTrue(x == (weekdays(enum) ^ weekend(enum)))
            self.assertTrue((weekdays(enum) & weekend(enum)) == enum["None"])
            self.assertTrue(weekdays(enum) == enum.Weekdays)
            self.assertTrue(weekend(enum) == enum.Weekend)
            self.assertTrue(weekdays(enum) != enum.Weekend)
            self.assertTrue(weekdays(enum) != weekend(enum))

        for e in [
                DaysInt, DaysShort, DaysLong, DaysSByte, DaysByte, DaysUShort,
                DaysUInt, DaysULong
        ]:
            enum_helper(e)

        for e in [DaysInt, DaysShort, DaysLong, DaysSByte]:
            z = operator.inv(e.Mon)
            self.assertEqual(type(z), e)
            self.assertEqual(z.ToString(), "-2")

        for (e, v) in [(DaysByte, 254), (DaysUShort, 65534),
                       (DaysUInt, 4294967294),
                       (DaysULong, 18446744073709551614)]:
            z = operator.inv(e.Mon)
            self.assertEqual(type(z), e)
            self.assertEqual(z.ToString(), str(v))

        self.assertRaises(ValueError, lambda: DaysInt.Mon & DaysShort.Mon)
        self.assertRaises(ValueError, lambda: DaysInt.Mon | DaysShort.Mon)
        self.assertRaises(ValueError, lambda: DaysInt.Mon ^ DaysShort.Mon)
        self.assertRaises(ValueError, lambda: DaysInt.Mon & 1)
        self.assertRaises(ValueError, lambda: DaysInt.Mon | 1)
        self.assertRaises(ValueError, lambda: DaysInt.Mon ^ 1)

        def f():
            if DaysInt.Mon == DaysShort.Mon: return True
            return False

        self.assertEqual(f(), False)

        self.assertTrue(not DaysInt.Mon == None)
        self.assertTrue(DaysInt.Mon != None)
Exemplo n.º 4
0
def main():

    env = Environment(NMBALLS)
    env.FrictionFactor = 1

    def create_view():
        form = EnvironmentDisplay()
        form.Renderer = Renderer(env)
        return form

    gui = GUI.ShowForm(Func[Form](create_view))

    x = list()
    y = list()
    model = None

    class Stopper(keras.callbacks.Callback):
        def on_batch_begin(self, batch, logs=None):
            char = readchar()
            if char == key.ESC or gui.IsCompleted:
                self.model.stop_training = True

    controller = Stopper()

    speed_diffs = list()

    actions = np.zeros((NMBALLS, 5), dtype='float32')
    actions[:, 0] = 1
    actions = actions.flatten()

    for e in range(0, 500):
        env.Reset()
        observation0 = None
        observation1 = None
        for i in range(0, 201):
            observation0 = observation1
            for s in range(0, 3):
                r = env.Step(actions)
            observation1 = list(r.Observation)

            if observation0 is not None:
                speed_diffs.append(speed_diff(observation0, observation1))
                x.append(enhance_input(observation0))
                y.append(observation1)

            if gui.IsCompleted: return

    model, loss = build_model(x[0], y[0], softmax=False)
    model.summary()

    nx = np.array(x, dtype='float32')
    ny = np.array(y, dtype='float32')
    nsd = np.array(speed_diffs, dtype='float32')
    print('Speed diff histogram:', np.histogram(nsd)[0])
    nsd = nsd.clip(0.05, 5)

    model.compile(optimizer=keras.optimizers.Adadelta(lr=5), loss=loss)
    model.fit(x=nx,
              y=ny,
              sample_weight=nsd,
              validation_split=0.2,
              verbose=2,
              epochs=400,
              batch_size=1000,
              shuffle=True,
              callbacks=[controller])

    state0 = observation1
    while not gui.IsCompleted:
        state0 = enhance_input(state0)
        state0 = np.expand_dims(state0, 0)
        state1 = model.predict(state0, batch_size=1)
        state1 = state1.clip(-1, 1)
        for i in range(0, env.Objects.Length):
            x = i * 6
            env.Objects[i].Position = PointF(state1[0, x], state1[0, x + 1])
            env.Objects[i].Speed = PointF(state1[0, x + 2], state1[0, x + 3])
            env.Objects[i].Acceleration = PointF(state1[0, x + 4],
                                                 state1[0, x + 5])
        time.sleep(.05)
        state0 = state1

    gui.Wait()
    if gui.Exception != None:
        print(gui.Exception)